Question

I have a view that has several editorfor/textboxfors

<% using(Html.BeginForm("New", "Controller", FormMethod.Post)) {%>
    <%: Html.ValidationSummary(false) %>
    <div>Address</div>
    <div><%: Html.EditorFor(model => model.Address)</div>
    <div>Zip Code</div>
    <div><%: Html.EditorFor(model => model.Zip)</div>
    <div>Total Mats</div>
    <div><%: Html.EditorFor(model => model.TotalMats)</div>
    <input type="image" src="<%: Url.Content("~/Content/Images/submit.png") %>" name="Submit" />
   <% } %>

Action:

public ActionResult Edit(int Id, FormCOllecion) {
    Event event = context.Event.FirstOrDefault(e => e.Id == Id);
    if (TryUpdateModel(event) {
       context.SaveChanges;
       return RedirectToAction("Index", "Controller", new {value = Id});
    }
    return View(event);
}

Model:

[Bind(Include="Name,Date")]
public class Event
{
     public int Id { get; set; }
     public string Name { get; set; }
     public DateTime Date { get; set; }
     public int Participants { get; set; }
     public int TotalMats { get; set; }
}

So The first two EditorFors are strings, while the last is a int (on the Model). That's one thing I noticed. I'm not sure if it's relevant. But everytime I would hit submit, the formcollection would show that there's a value for the TotalMats field. But once it passes through the TryUpdateModel it wouldn't update the property of the model. Is there anything I'm missing? It has been like that on some of my pages that uses a property that is an int type. I had to do a manual FormCollection["key"] retrieval but i'm forced to do additional validations just to make sure it doesn't bomb out for non integer values.

Was it helpful?

Solution

Isn't the problem your BindAttribute?

http://msdn.microsoft.com/en-us/library/system.web.mvc.bindattribute.include

According to the documentation only the properties listed in the Include list will be bound/updated. Your ints aren't on the list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top