2011-05-20 73 views
0

我一直在閱讀關於UpdateModel()和自定義模型粘合劑的所有信息,但我仍然無法弄清楚。似乎這些都是對此的簡單回答。如何在MVC3中綁定嵌套模型?

我有一個叫做用戶的類,我用戶遍佈我的MVC Web應用程序。

public class User 
{ 
    [Key] 
    public int ID { get; set; } 

    [Required] 
    public string Name { get; set; } 

    [Required] 
    [Email] 
    public string Email { get; set; } 

    [Required] 
    public string Password { get; set; } 

    public virtual ICollection<User> WorkTasks { get; set; } 
} 

中,然後在它在幾個地方一WorkTask:

public class WorkTask 
{ 
    [Key] 
    public int ID { get; set; } 
    [Required] 
    public string Name { get; set; } 
    public DateTime? DesiredStartDate { get; set; } 
    public DateTime? DesiredEndDate { get; set; } 


    public TimeSpan? DesiredTimeSpent { get; set; } 

    public virtual ICollection<WorkTaskTag> Tags { get; set; } 
    public virtual ICollection<WorkTaskPeriod> Periods { get; set; } 

    public virtual ICollection<User> InvolvedUsers { get; set; } 
    public virtual User CreatedBy { get; set; } 
    public virtual User AssignedTo { get; set; } 

    public string UserNameAssignedTo 
    { 
     get 
     { 
      if(AssignedTo!=null) 
      return AssignedTo.Name; 
      return CreatedBy.Name; 
     } 
    } 

    public string TotalTimeSpent { 
     get 
     { 
      var concretePeriods = Periods 
       .Where(i => i.StartDate.HasValue && i.EndDate.HasValue); 
      if (concretePeriods != null && concretePeriods.Count() > 0) 
      { 
       TimeSpan ts = new TimeSpan(); 
       foreach (var p in concretePeriods) 
       { 
        var t=p.EndDate.Value-p.StartDate.Value; 
        ts.Add(t); 
       } 
       TimePeriodHelpers help = new TimePeriodHelpers(); 
       return help.GetTimeFormat(ts); 
      } 
      return "0:00"; 
     } 
    } 
} 

讓我怎麼做創建模板此WorkTask,允許用戶類綁定到WorkTask多地方呢?

這是我非常劣質的嘗試:

[HttpPost] 
    public ActionResult Create(WorkTask worktask) 
    { 
     LoadUsers(); 
     string assignedto=Request["AssignedTo"]; 
     var user = db.Users.First(i => SqlFunctions.StringConvert((double)i.ID) == assignedto); 
     UpdateModel<User>(user); 
     if (ModelState.IsValid) 
     { 
      db.WorkTasks.Add(worktask); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     return View(worktask); 
    } 

和視圖:

@using (Html.BeginForm()) { 
@Html.ValidationSummary(true) 
<fieldset> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Name) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Name) 
     @Html.ValidationMessageFor(model => model.Name) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.DesiredStartDate,"Desired Start Date") 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.DesiredStartDate) 
     @Html.ValidationMessageFor(model => model.DesiredStartDate) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.DesiredEndDate,"Desired End Date") 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.DesiredEndDate) 
     @Html.ValidationMessageFor(model => model.DesiredEndDate) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.DesiredTimeSpent,"Desired Time Spent") 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.DesiredTimeSpent) 
     @Html.ValidationMessageFor(model => model.DesiredTimeSpent) 
    </div> 
    <div class="editor-label"> 
     @Html.LabelFor(model => model.AssignedTo,"User Assigned To") 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownListFor(i=>Model.AssignedTo,(IEnumerable<SelectListItem>)ViewBag.Users) 
     @Html.ValidationMessageFor(model => model.AssignedTo) 
    </div> 
    <p> 
     <input type="submit" value="Create" /> 
    </p> 

回答

0

,而不是試圖綁定嵌套的用戶對象,我做了基於視圖模型的形式和控制器,它只是有用戶名。然後我假設,如果ViewModel驗證正確,比我可以繼續並堅持WorkTask和嵌套的用戶對象。