ASP.NET MVC - using model property as form, how can I post to action?

Posted by Ryan Peters on Stack Overflow See other posts from Stack Overflow or by Ryan Peters
Published on 2011-01-12T01:37:38Z Indexed on 2011/01/12 1:53 UTC
Read the original article Hit count: 571

Filed under:
|
|

Consider the following model:

public class BandProfileModel
{
    public BandModel Band { get; set; }
    public IEnumerable<Relationship> Requests { get; set; }
}

and the following form:

<% using (Html.BeginForm()) { %>
    <%: Html.EditorFor(m => m.Band) %>
    <input type="submit" value="Save Band" />
<% } %>

which posts to the following action:

public ActionResult EditPost(BandProfileModel m, string band)
{
    // stuff is done here, but m is null?

    return View(m);
}

Basically, I only have one property on my model that is used in the form. The other property in BandProfleModel is just used in the UI for other data. I'm trying to update just the Band property, but for each post, the argument "m" is always null (specifically, the .Band property is null).

It's posting just fine to the action, so it isn't a problem with my route. Just the data is null.

The ID and name attributes of the fields are BAND_whatever and Band.whatever (whatever being a property of Band), so it seems like it would work...

What am I doing wrong? How can I use just one property as part of a form, post back, and have values populated via the model binder for my BandProfileModel property in the action? Thanks.

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET