简体   繁体   中英

How to bind a nested model in MVC3?

I've been reading all about UpdateModel() and custom model binders and i still cant figure this out. Seems like theres gotta be a simple answer to this.

I have a class called user that i user all over my MVC Web app.

 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; }
}

And then a WorkTask that has it in a few places:

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";
        }
    }
}

So how do i make a create template for this WorkTask that allows the User class to be bound to the WorkTask in multiple places?

Here's my very shoddy attempt:

[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);
    }

and the view:

@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>

Instead of trying to bind the nested User object, i made the form and controller based upon a ViewModel which just had the UserID. I then assumed that if the ViewModel validated correctly, than i can go ahead and persist the WorkTask and nested User objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM