How to allow the post action method to use a subobject of the original view model as a parameter?

This is my model:

public class IndexViewModel
{
    public FilterConditions conditions { get; set }
    public IEnumerable<SelectListItem> Countries { get; set }
}

public class FilterConditions
{
    public string condition11 { get; set }
    // ...
}

And I have an action method Index, for example:

public ActionResult Index()
{
    var model = new IndexViewModel();

    // fill the model here with default values

    return View(model);
}

The view displays a form with filter conditions as input types.

Now I want the message back from this form to be processed using this action method:

[HttpPost]
public ActionResult Index(FilterConditions model)
{
    // do some magic with model and return another view here
}

and it really works (I set a breakpoint in the method and is called), but the properties of my model are always empty (default values), while they should contain the values ​​that were submitted by the form.

When I change the action method as follows:

[HttpPost]
public ActionResult Index(IndexViewModel model)
{
    // do some magic with model.conditions and return another view here
}

Everything works as it should, but it’s not “right” (IMHO), since I don’t need the “Countries” list when I return, I only need the selected country (which is one of the conditions).

( ), ?

Btw, ASP.NET MVC 2 ( , , , MVC 3, ).

( " " dropdownlists viewmodels asp.net mvc, , , , "" . , ( ), , . " " , .)

:

, [Bind] " ". . ( , ) .

, ( ajax), , . ?

+2
2

.

-, ( ), :

[HttpPost]
public ActionResult Index(FilterConditions filterConditions)
{
    // do some magic with model and return another view here
    // now the filterConditions variable actually contains values!
}

, ( Conditions /null ). -, modelbinder .

, , , - . .

Edit: : (aspx):

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyProject.Models.IndexViewModel>" %>

<%-- ... more stuff here ... --%>

<% using (Html.BeginForm())
   {%>
    <%= Html.ValidationSummary(true)%>

    <fieldset>
        <div class="editor-label">
            <%= Html.LabelFor(model => model.FilterConditions.Country)%>
        </div>
        <div class="editor-field">
            <%= Html.DropDownListFor(model => model.FilterConditions.Country, Model.Countries)%>
            <%= Html.ValidationMessageFor(model => model.FilterConditions.Country)%>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.FilterConditions.Make)%>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.FilterConditions.Make)%>
            <%= Html.ValidationMessageFor(model => model.FilterConditions.Make)%>
        </div>

        <%-- ... more fields inserted here ... --%>

        <p>
            <input type="submit" value="  Search...  " />
        </p>
    </fieldset>

<% } %>
+4

, fretje: . "IndexViewModel" "Index", DropDownList (, DropDown):

public class IndexViewModel : Index
{
   //public  int value { get; set; }
   public   List<System.Web.Mvc.SelectListItem> items { get; set; }
}

public class Index
{
    public int value { get; set; }
}


class DropDownList
{
   public List<System.Web.Mvc.SelectListItem> GetDropDownList()
   {
       List<System.Web.Mvc.SelectListItem> result = new List<System.Web.Mvc.SelectListItem>();
       result.Add(new System.Web.Mvc.SelectListItem
       {
           Value = "1",
           Text = "Apple"
       });
       result.Add(new System.Web.Mvc.SelectListItem
       {
           Value = "2",
           Text = "Milk"
       });
       return result;
   }
}

- Test() Test (Models.Index), IndexViewModel postback IndexModel:

public ActionResult Test()
{
    var result =
        new Models.IndexViewModel
        {
            value = 1,
            items = new Models.DropDownList().GetDropDownList()
        };
    return View(result);
}
[HttpPost]
public ActionResult Test(Models.Index posatback)
{
    return View();
}

():

<% using (Html.BeginForm()) {%>
    <%: Html.ValidationSummary(true) %>        
    <fieldset>
        <legend>Fields</legend>            
        <div class="editor-field">
                    <%: Html.DropDownListFor(m=>m.value, Model.items )%>
        </div>            
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
<% } %>

! , .:)


,

[HttpPost]
public ActionResult Index([Bind(Exclude="Countries")]IndexViewModel model)
{
    // do some magic with model.conditions and return another view here
}

~ SelectListItem ViewModel, ViewModel , , DropDownListFor, :

<%: Html.DropDownListFor(item.WeaponID, MyApplication.Models.DropDownList.GetDropDownList() )%>

. , , : http://maidot.blogspot.com/2011/04/aspnet-mvc-viewdropdownlistfor.html

, :)

+1

All Articles