Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5949033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:11:33+00:00 2026-05-22T17:11:33+00:00

I’ve been reading all about UpdateModel() and custom model binders and i still cant

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:11:34+00:00Added an answer on May 22, 2026 at 5:11 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.