0

I have my model for my users in MVC3:

public class User
{
    [Key]
    public virtual Guid UserId { get; set; }

    [Required]
    public virtual String Username { get; set; }

    [DataType(DataType.MultilineText)]
    public virtual String Comment { get; set; }

   ..loads of other properties

This class contains loads of info that I don't really need in an edit screen. Let's say I only wanted to edit the comment part of the user. I tried to create a viewmodel like this but not sure if im going about this right

view model

 public class UserViewModel
  {
    public string notes;

  }

Now I have this in my controller:

public ActionResult Edit(Guid id)
        {
           User user = Context.Users.Where(x => x.UserId == id).First();
          UserViewModel uvm = new UserViewModel();
          uvm.notes = user.Comment;
            return View(uvm);
        }

This part seems to work ok and passes the info through if it exists. The edit view is like this:

@model core.Areas.Manage.Models.UserViewModel
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>User Edit</legend>

        @Html.TextBoxFor(model => model.notes)


        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

Which shows the notes/comment from the controller. However, if i press submit, the model is nulled - The model.notes is null at this point:

 [HttpPost]
        public ActionResult Edit(Guid id, UserViewModel model)
        {

Am i doing something completely wrong? In the past I simply passed the whole model around, but this isn't really ideal. Im kind of taking a stab in the dark here, just thought i'd try to implement it myself before turning to SO!

JustAnotherDeveloper
  • 3,167
  • 11
  • 37
  • 68
  • 2
    How does your POST action look like, where the model is nulled? – nemesv Aug 10 '12 at 15:38
  • What do you mean by "the model is nulled"? Nulled where? When you press submit and HTTP POST request is raised to the server, the MVC routes this to the Edit method which only takes the id (nothing else in the model is passed through because it can't, you've only allowed the id through) – Colin Mackay Aug 10 '12 at 15:39
  • Use Chrome or Firebug in Firefox, or the dev tools in IE to look at the actual request sent from the browser. Is it actually sending the data in the request? Is it actually going to the correct URL? – Gromer Aug 10 '12 at 15:46

2 Answers2

3

I think it might be because you don't have {get;set;} declared in your view model. See this post: { get; set; } used in ViewModel

Community
  • 1
  • 1
Justin Harvey
  • 14,446
  • 2
  • 27
  • 30
  • Yes, the model binding is not working, and this is probably why. – Facio Ratio Aug 10 '12 at 15:47
  • Seems to work. I wasn't aware I needed to add them in the viewmodel! I take it that this is probably still the best approach to only wanting to edit a couple of fields without pushing the whole model around? – JustAnotherDeveloper Aug 10 '12 at 15:48
  • The only thing I would say is, I wouldn't sweat too much about using a much bigger model than you are actually displaying on the page. As long as you have editted the View to only use the select fields you are interested in, then you are not actually sending anything extra down the wire. – Justin Harvey Aug 10 '12 at 15:57
2

You declared notes as a field. Model binding wont work with fields. you should declare it as a property

public class UserViewModel
{
  public string notes  {set;get;}
}

The ASP.NET MVC framework includes default model binder implementation named the DefaultModelBinder, which is designed to effectively bind most model types. It does this by using relatively simple and recursive logic for each property of the target model:

Shyju
  • 214,206
  • 104
  • 411
  • 497