Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC model binding keeps values NULL

I'm trying to get custom model binding to work, but for some reason the values aren't set. The code seems ligit when comparing it to working code, but still it doesnt bind. I guess it some trivial thing i'm missing.

Custom model:

//Cluster is from Entity Framework

//BaseViewModelAdmin defines:
public List<KeyValuePair<string, string>> MenuItems;
public IPrincipal CurrentUser = null;
public Foundation Foundation; //also from Entity Framework

public class AdminClusterCreateModel : BaseViewModelAdmin
{
    public Cluster Item;
    public AdminClusterCreateModel()
    {
        Item = new Cluster();
    }
}

The view form looks like:

@using (Html.BeginForm()) {
  @Html.ValidationSummary(true)

  <fieldset>
      <legend>Cluster</legend>

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


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

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

And the controller:

[HttpPost]
public ActionResult Create(AdminClusterCreateModel model, FormCollection form)
{
    if(ModelState.IsValid) //true
    {
        var test = form["Item.Name"]; //Value is correct from form (EG: Test)
        UpdateModel(model);  //no error
    }

    //At this point model.Item.Name = null <--- WHY?

    return View(model);
}

Cluster on request

public partial class Cluster
{
    public Cluster()
    {
        this.Team = new HashSet<Team>();
    }

    public long Id { get; set; }
    public System.DateTime Created { get; set; }
    public System.DateTime Modified { get; set; }
    public bool Active { get; set; }
    public long FoundationId { get; set; }
    public string Name { get; set; }

    public virtual Foundation Foundation { get; set; }
    public virtual ICollection<Team> Team { get; set; }
}
like image 270
Hugo Delsing Avatar asked Apr 16 '13 10:04

Hugo Delsing


People also ask

How does model binding works in MVC?

Model binding is a well-designed bridge between the HTTP request and the C# action methods. It makes it easy for developers to work with data on forms (views), because POST and GET is automatically transferred into a data model you specify. ASP.NET MVC uses default binders to complete this behind the scene.

Does MVC use data binding?

MVC doesn't use data bindings like old web api. You have to use model bindings in a MVC or MVVM approach.

Is MVC model binding case sensitive?

The action in the mvc controller is trying to bind it to a similar model, but with lower-case first letters. It used to work fine, but after some heavy refactoring (apparently unrelated), it doesn't. To be precise, it works on POST, but not on PUT. And still, it depends on the properties.

How do you deal with null models?

In order to simplify your code you should utilize the Null Object pattern. Instead of using null to represent a non existing value, you use an object initialized to empty/meaningless values. This way you do not need to check in dozens of places for nulls and get NullReferenceExpections in case you miss it.


1 Answers

DefaultModelBinder works explicitly on 'Properties', not on 'Fields'

Changing public Cluster Item to public Cluster Item {get; set;} in AdminClusterCreateModel should do the trick.

public class AdminClusterCreateModel : BaseViewModelAdmin
{
    public Cluster Item {get; set;}

    public AdminClusterCreateModel()
    {
        Item = new Cluster();
    }
 }

Regards

like image 149
shakib Avatar answered Oct 16 '22 21:10

shakib