tags:

views:

64

answers:

1

I'm experimenting with the MVC beta 2 and have the following method in a controller:

[HttpPost]
public ActionResult Create(Issue issue) {
    if(TryUpdateModel(issue, "Model", new [] {"Title", "Description"})) {
        ServiceCaller.PutIssue(issue);
        return RedirectToAction("Index");
    }
    return RedirectToAction("Create");
}

TryUpdateModel always fails and ModelState has an error under a Key called "Id" which says that "A value is required". The View contains no Id input field and I understood that my "include" argument on the TryUpdateModel should have ignored anything other than the explicitly included fields anyway. Changing the method signature to the following fixes the problem but I'd like to understand how the "Id" field is getting included in the first place.

public ActionResult Create([Bind(Exclude = "Id")]Issue issue)

For completeness, here's the View (it's a shared view in the EditorTemplates folder):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Tracker.Processing.Model.Issue>" %>
<%  using (Html.BeginForm(Model.Id > 0 ? "Edit" : "Create", "Issue", FormMethod.Post)) { %>
<p>
    <%= Html.LabelFor(i => i.Title) %>
    <%= Html.EditorFor(i => i.Title) %>
</p>
<p>
    <%= Html.LabelFor(i => i.Description) %>
    <%= Html.TextArea("Description", Model.Description, new{ style = "width: 100%;" }) %>
</p>
<p>
    <input type="submit" value="Save" />
</p>
<% } %>

Any ideas?

+2  A: 

Its attempting to match the Id in the route to the one on your object as the one in the route is null and the Id on your object is not nullable you get the error

Irritating isn't it

Anthony Johnston
argh! didn't think about the routes...
grenade
come to think of it, it should of been obvious because I use the same view for edit which gets its id from the route. doh.
grenade