2010-12-09 48 views
0

我有一個時間表應用程序,它有一個View,用戶可以選擇客戶和任務並將它們添加到動態表格中。該表格填寫了填寫工作小時數的任務和輸入字段。模型沒有在MVC 2應用程序中更新

爲了在動態表中添加新任務我使用jQuery,因此savenewtask按鈕不是提交按鈕。相反,我有一個適當的提交按鈕,用於保存填入的小時數。

該視圖被強制類型爲名爲TimesheetViewModel的模型(請參見下文)。控制器將模型傳遞給View,然後將輸入字段綁定到模型中的屬性。

但是,當我用提交按鈕提交併嘗試更新控制器中的模型時,它不會更新。從Nerddinner教程(我正在使用它學習MVC)看來,模型應該使用UpdateModel()時綁定的表單字段中的值自動更新。但事實並非如此。我究竟做錯了什麼?

這裏是所有相關代碼:

查看:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
<script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script> 
    <script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      //Hook onto the MakeID list's onchange event 
      $("#CustomerId").change(function() { 
       //build the request url 
       var url = "Timesheet/CustomerTasks"; 
       //fire off the request, passing it the id which is the MakeID's selected item value 
       $.getJSON(url, { id: $("#CustomerId").val() }, function (data) { 
        //Clear the Model list 
        $("#TaskId").empty(); 
        //Foreach Model in the list, add a model option from the data returned 
        $.each(data, function (index, optionData) { 
         $("#TaskId").append("<option value='" + optionData.Id + "'>" + optionData.Name + "</option>"); 
        }); 
       }); 
      }).change(); 
     }); 

    </script> 
    <h2>Index</h2> 

    <% using (Html.BeginForm()) 
     {%> 
    <%: Html.ValidationSummary(true) %> 
    <fieldset> 
     <legend>Fields</legend> 
     <div> 
      <label for="Customers"> 
       Kund:</label> 
      <%:Html.DropDownListFor(m => m.Customers, new SelectList(Model.Customers, "Id", "Name"), "Välj kund...", new { @id = "CustomerId" })%> 
      &nbsp;&nbsp; 
      <label for="Tasks"> 
       Aktiviteter:</label> 
      <select id="TaskId"> 
      </select> 
     </div> 
     <p> 
      <input type="button" value="Save new task" id="savenewtask" />    
     </p> 

     <table width="100%"> 
     <%--<% foreach (var task in Model.Tasks)--%> 
     <% foreach (var task in Model.WeekTasks) 
      { %> 
     <tr> 
      <td> 
       <%: task.Customer.Name %> 
      </td> 
      <td> 
       <%: task.Name %> 
      </td> 
      <td> 
       <% foreach (var ts in task.TimeSegments) 
        { %> 
       <input class="hourInput" type="text" size="2" id="<%: ts.Task.CustomerId + '_' + ts.TaskId + '_' + ts.Date %>" 
        value="<%: ts.Hours %>" /> 
       <% } %> 
      </td> 
     </tr> 
     <% } %> 
    </table> 
    <input type="submit" value="Save hours" id="savehours" /> 
    </fieldset> 
    <% } %> 

</asp:Content> 

從控制器:

private TimesheetViewModel _model; 

public TimesheetController() 
{ 
    _model = new TimesheetViewModel(); 
} 

public ActionResult Index() 
{ 

    return View(_model); 
} 

[HttpPost] 
public ActionResult Index(FormCollection collection) 
{ 
    try 
    { 
     UpdateModel(_model); 
     _model.Save(); 
     return View(_model); 
     //return RedirectToAction("Index"); 
    } 
    catch 
    { 
     return View(); 
    } 
} 

視圖模型:

public class TimesheetViewModel 
{ 
    private TimesheetContainer _model; //TimesheeContainer is an Entity Framework model 

    public TimesheetViewModel() 
    { 
     _model = new TimesheetContainer(); 
    } 

    public IList<Customer> Customers 
    { get { return _model.Customers.ToList(); } } 

    public IList<Task> Tasks 
    { get { return _model.Tasks.ToList(); } } 

    public IList<Task> WeekTasks 
    { 
     get 
     { 
      //Get the time segments for the current week 
      DateTime firstDayOfWeek = DateTime.Parse("2010-12-05"); 
      DateTime lastDayOfWeek = DateTime.Parse("2010-12-13"); 

      List<TimeSegment> timeSegments = new List<TimeSegment>(); 
      foreach (var timeSegment in _model.TimeSegments) 
      { 
       if(timeSegment.DateTimeDate > firstDayOfWeek && timeSegment.DateTimeDate < lastDayOfWeek) 
        timeSegments.Add(timeSegment); 
      } 
      //Group into tasks 
      var tasks = from timeSegment in timeSegments 
         group timeSegment by timeSegment.Task 
         into t 
         select new { Task = t.Key }; 
      return tasks.Select(t => t.Task).ToList(); 
     } 
    } 

    public IList<TimeSegment> TimeSegments 
    { get { return _model.TimeSegments.ToList(); } } 

    public void Save() 
    { 
     _model.SaveChanges(); 
    } 


    public void AddTimeSegments(Task task) 
    { 
     _model.AddToTasks(task); 
     _model.SaveChanges(); 
    } 

} 

部分類來獲得任務爲特定的本週(此時只用於測試假人周):

public partial class TimeSegment 
{ 
    public DateTime DateTimeDate 
    { get { return DateTime.Parse(Date); } } 
} 

爲什麼模型中未更新,並且我能更改,使其工作?

回答

0

在你的第一個ActionResult索引()上放置一個斷點,是否在你提交時調用?你可能需要[HttpGet],否則我認爲它同時得到。

+0

我試過了,但沒有,那個人永遠不會被調用。 – Anders 2010-12-09 19:18:52