tags:

views:

142

answers:

3

Hi ,

Am using strongly typed view to show a complex object in a data entry/edit form. for eg: Model.UserInformation.Name, Model.LivingPlace.FacilitiesSelectList, Model.Education.DegreesList... etc. These information are shown in multiselect listbox, grids.. etc. User can change the information in the edit screen. Is there any way to post the Model object with user changes to controller on sumbit button click. Please suggest.

Regards, SHAN

+2  A: 

The same object instance that has been passed to the view: No. ASP.NET MVC uses a default Model binder to instantiate new action parameters from request values. So for example if you had the following action method:

public ActionMethod DoWork(User model)
{
    return View();
}

public class Address
{
    public string Street { get; set; }
}

public class User
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Address[] Addresses { get; set; }
}

the binder will look in the request and try to bind model values. You could to the following in your View:

<%= Html.TextBox("FirstName") %>
<%= Html.TextBox("LastName") %>
<%= Html.TextBox("Addresses[0].Street") %>
<%= Html.TextBox("Addresses[1].Street") %>

This will automatically populate the values of your model in the controller action.

To avoid mass assignment of properties that shouldn't be bound from request values it is always a good idea to use the BindAttribute and set Exclude or Include properties.

Darin Dimitrov
A: 

Use <input type="text" name="UserInformation.Name"><input> to bind to subobjects.

Malcolm Frexner
A: 

Check this question.

çağdaş