MVC3 how to post a list within a class in controller?

后端 未结 3 2011
傲寒
傲寒 2021-01-27 03:42

I have a class:

public class CarList
{
    public int quantity{get;set;}
    public List Cars {get;set;}
}

public class Car {
    public string Name          


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-27 04:38

    Use an EditorTemplate and you will be good.

    Create a Folder Called "EditorTemplates" and create a view (the editor template) with the name Car.cshtml

    enter image description here

    Now Add the below code to this new view.

    @model Car
    

    @Html.TextBoxFor(x => x.Name)

    Now in your Main View, Use the Html.EditorFor HTML helper method to call this editor template

    @model SO_MVC.Models.CarList
    

    CarList

    @using (Html.BeginForm()) {

    Quanitty

    @Html.TextBoxFor(x => x.quantity) @Html.EditorFor(x=>x.Cars) }

    Now have an HTTPPOst action method to accept the form posting

    [HttpPost]
    public ActionResult CarList(CarList model)
    {
       //Check model.Cars property now.
    }
    

    You will see the results now enter image description here

提交回复
热议问题