Question

I must have something incorrectly setup as I can't get the UpdateModel function to correctly update my model based on information passed in via a FormCollection.

My View looks like:

@model NSLM.Models.Person
@{
    ViewBag.Title = "MVC Example";
}
<h2>My MVC Model</h2>
<fieldset>
    <legend>Person</legend>
    @using(@Html.BeginForm())
    {
        <p>ID: @Html.TextBox("ID", Model.ID)</p>
        <p>Forename: @Html.TextBox("Forename", Model.Forename)</p>
        <p>Surname: @Html.TextBox("Surname", Model.Surname)</p>
        <input type="submit" value="Submit" />
    }  
</fieldset>

My model is:

    namespace NSLM.Models
    {
        public class Person
        {
            public int ID;
            public string Forename;
            public string Surname;
        }
    }

and my controller is:

        [HttpPost]
        public ActionResult Details(FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here                
                Models.Person m = new Models.Person();

                // This doesn't work i.e. the model is not updated with the form values
                TryUpdateModel(m);

                // This does work 
                int.TryParse(Request.Form["ID"], out m.ID);
                m.Forename = Request.Form["Forename"];
                m.Surname = Request.Form["Surname"];

                return View(m);
            }
            catch
            {
                return View();
            }
        }

as you can see if I manually assign each property it works fine, so what have I not set that would get the model to be updated with the form values?

Thanks,

Mark

Was it helpful?

Solution

Replace fields with properties in your model, i.e.:

namespace NSLM.Models
{
    public class Person
    {
        public int ID {get; set;}
        public string Forename {get; set;}
        public string Surname {get; set;}
    }
}

OTHER TIPS

By the time the call gets to the action method any automatic model binding has already been performed. Try changing the input parameter of your action method to accept a Person instance. In that case the model binder will try to create the instance and populate it from the values passed by your form.

try this :

view :

@model NSLM.Models.Person
@{
    ViewBag.Title = "MVC Example";
}
<h2>My MVC Model</h2>
<fieldset>
    <legend>Person</legend>
    @using(@Html.BeginForm())
    {
         @Html.HiddenFor(model => model.ID)

        <p>Forename: @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </p>
        <p>Surname: @Html.EditorFor(model => model.Surname)
            @Html.ValidationMessageFor(model => model.Surname)
        </p>
        <input type="submit" value="Submit" />
    }  
</fieldset>

Controller :

[HttpPost]
public ActionResult Details(Person p)
{
    if (ModelState.IsValid)
    {
        db.Entry(p).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(p);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top