ASP.NET MVC 5将模型和模型列表发送到控制器

时间:2015-12-28 11:30:10

标签: c# asp.net-mvc

型号:

public class Room
{
    public string Name { get; set; }
}

public class Building
{
    public string Name { get; set; }
}

public class ViewModel
{
    public Room room {get;set;}
    public List<Room> rooms { get; set; }
    public Building  building { get; set; }
}

控制器:

public class BuildingController : Controller
{
    // GET: Building
    public ActionResult Index()
    {
        return View();
    }

    [HttpGet]
    public ActionResult Create()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Create(ViewModel viewModel)
    {
        // Here I save the model in DB
        return View(viewModel);
    }
}

查看:

@model Test_asp.net.Models.ViewModel
@{
    ViewBag.Title = "Create";
}

@using (Html.BeginForm())
{
    <h2>Create Building</h2>
    <div class="form-group clone form-inline">
        <div>
            @Html.EditorFor(model => model.building.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.building.Name, "", new { @class = "text-danger" })
            <a href="#" class="remove">Remove</a>
        </div>
        <div>
            @Html.EditorFor(model => model.room.Name, new { htmlAttributes = new { @Name = "[0]Room" } })
            @Html.ValidationMessageFor(model => model.building.Name);
            <a href="#" class="remove">Remove</a>
        </div>
        <div>
            @Html.EditorFor(model => model.room.Name, new { htmlAttributes = new { @Name="[1]Room" } })
            @Html.ValidationMessageFor(model => model.building.Name)
            <a href="#" class="remove">Remove</a>
        </div>
    </div>

    <input type="submit" />
}

查看输出:

<form action="/Building/create" method="post">    <h2>Create Building</h2>
    <div class="form-group clone form-inline">
        <div>
            <input class="form-control text-box single-line" id="building_Name" name="building.Name" type="text" value="" />
            <span class="field-validation-valid text-danger" data-valmsg-for="building.Name" data-valmsg-replace="true"></span>
            <a href="#" class="remove">Remove</a>
        </div>
        <div>
            <input Name="[0]Room" class="text-box single-line" id="room_Name" name="room.Name" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="building.Name" data-valmsg-replace="true"></span>;
            <a href="#" class="remove">Remove</a>
        </div>
        <div>
            <input Name="[1]Room" class="text-box single-line" id="room_Name" name="room.Name" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="building.Name" data-valmsg-replace="true"></span>
            <a href="#" class="remove">Remove</a>
        </div>
    </div>
    <input type="submit" />
</form>

控制器中的create方法应该接收带有构建对象和List房间的viewModel。实际上我收到一个Building对象而且我不知道为什么房间列表为空?

3 个答案:

答案 0 :(得分:1)

尝试更改

中的输入名称
@Name="[0]Room"

rooms[0].Name

您必须包含列表属性rooms的名称,因为Action方法中的属性不是List<Room>,而是ViewModel viewModel

由于您要删除列表中的项目,而不是使用后续索引0,1,2 ......等(应该是不间断的)使用索引:

<input type="hidden" name="rooms.Index" value="AAA" />
<input type="text" name="rooms[AAA].Name" value="Room 1" />

<input type="hidden" name="rooms.Index" value="AAB" />
<input type="text" name="rooms[AAB].Name" value="Room 2" />

您可以即时生成索引的值。

您可以阅读Phil Haack的帖子here

中的更多内容

答案 1 :(得分:0)

我更喜欢使用带随机键的字典(刻度,指导等)

型号:

    public class Room
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class ViewModel
    {
        public Room room {get;set;}
        public Dictionary <string, Room> rooms { get; set; }
    }

查看:

<div>
foreach(var room in rooms){
    <div>
        @Html.HiddenFor(model => model.rooms[room.Key].Id)
        @Html.EditorFor(model => model.rooms[room.Key].Name, new { htmlAttributes = new { @class = "form-control" } })
        <a href="#" class="remove" data-id="@Model.rooms[room.Key].Id">Remove</a>
    </div>
}
</div>

答案 2 :(得分:0)

更改

public List<Room> rooms { get; set; }

public IList<Room> rooms { get; set; }

然后使用

  @for (int i=0; i<=3;i++)
    {
        <div> 
            @Html.Hidden("rooms.Index", null,new { Value = i})
            @Html.EditorFor(m=>m.rooms[i].Name)
            @Html.ValidationMessageFor(m=>m.rooms[i].Name);
            <a href="#" class="remove">Remove</a>
        </div>
    }

如果用户删除任何行,则从1渲染非顺序索引。

 @for (int i=1; i<=3;i++)
    {
        <div> 
            @Html.Hidden("rooms.Index", null,new { Value = i})
            @Html.EditorFor(m=>m.rooms[i].Name)
            @Html.ValidationMessageFor(m=>m.rooms[i].Name);
            <a href="#" class="remove">Remove</a>
        </div>
    }

这是您将生成的HTML

            <input value="1" id="rooms_Index" name="rooms.Index" type="hidden">
            <input class="text-box single-line" id="rooms_1__Name" name="rooms[1].Name" value="" type="text">
            <span class="field-validation-valid" data-valmsg-for="rooms[1].Name" data-valmsg-replace="true"></span>;
            <a href="#" class="remove">Remove</a>

            <input value="2" id="rooms_Index" name="rooms.Index" type="hidden">
            <input class="text-box single-line" id="rooms_2__Name" name="rooms[2].Name" value="" type="text">
            <span class="field-validation-valid" data-valmsg-for="rooms[2].Name" data-valmsg-replace="true"></span>;
            <a href="#" class="remove">Remove</a>

            <input value="3" id="rooms_Index" name="rooms.Index" type="hidden">
            <input class="text-box single-line" id="rooms_3__Name" name="rooms[3].Name" value="" type="text">
            <span class="field-validation-valid" data-valmsg-for="rooms[3].Name" data-valmsg-replace="true"></span>;
            <a href="#" class="remove">Remove</a>