MVC表单在控制器上发布null

时间:2015-12-22 23:20:38

标签: asp.net-mvc asp.net-mvc-partialview

构建一个允许用户添加无限行的表单。这是通过对控制器的ajax调用来完成的,该控制器添加了部分视图(每次点击一次)。

我使用Html.BeginCollectionItem索引部分视图中的每个输入。

我可以添加一堆项目并单击提交,我可以看到表单是在fiddler中发布的,但是控制器显示了一个null表单。如果我从List<>更改帖子控制器类型对于奇异的AccessoryVM,我得到了第一个项目。

我尝试将List视图中的模型类型从List交换到IEnumerable,没有变化。

指数:

@model List<EmployeeHardwareRequest.Models.ViewModels.AccessoryVM>

@{
    ViewBag.Title = "Select Accessories";
}

<h2>Accessories</h2>


    @using (Html.BeginForm("Index", "Accessory", FormMethod.Post, new { @class = "form-horizontal" }))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div id="form-body">
            @foreach (var item in Model)
            {
                @Html.Partial("_NewItem")
            }
        </div>

        <div class="row">
            <div class="col-md-6">
                <button id="add-item" class="btn btn-primary">Add Another Item</button>
            </div>
            <div class="col-md-6">
                <input type="submit" value="Select Software" class="btn btn-default pull-right" />
            </div>
        </div>
    }

部分:

@model EmployeeHardwareRequest.Models.ViewModels.AccessoryVM

<div class="form-group col-md-3">
    <div>
        @Html.DropDownListFor(model => model.SelectedAccessory, Model.AccessoryDdl, new { @class = "form-control accessoryList" })
    </div>
</div>

<div class="form-group col-md-8">
    <div>
        @using (Html.BeginCollectionItem("AccessoryVM"))
        {
            @Html.EditorFor(model => model.ProductLink, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter link to web page" } })
            @Html.ValidationMessageFor(model => model.ProductLink, "", new { @class = "text-danger" })
        }
    </div>
</div>
<div class="col-md-1">
    <button type="button" id="deleteItem" class="btn btn-danger">
        <span class="glyphicon glyphicon-remove"></span>
    </button>
</div>

控制器: 指数:         公共ActionResult索引()         {             List modelList = new List();

        List<Accessory> accessories = _db.Accessory.ToList();  // change this to use the accessoryDTO

        List<int> monitorSizeList = new List<int>  // replace this with data from db table.
        {
            20, 22, 24, 27
        };

        model.AccessoryDdl = new SelectList(accessories, "AccessoryId", "ItemDescription", selectedAcc);
        model.MonitorSizeDdl = new SelectList(monitorSizeList);
        return View(modelList);
    }

获取部分视图:

    public ActionResult AddItem(int index)
    {
        AccessoryVM model = new AccessoryVM();
        int selectedAcc = 1;

        List<Accessory> accessories = _db.Accessory.ToList();  // change this to use the accessoryDTO

        List<int> monitorSizeList = new List<int>  // replace this with data from db table.
        {
            20, 22, 24, 27
        };

        //model.lineIndex = index++;

        model.AccessoryDdl = new SelectList(accessories, "AccessoryId", "ItemDescription", selectedAcc);
        model.MonitorSizeDdl = new SelectList(monitorSizeList, "Select Monitor Size");
        return PartialView("_NewItem", model);
    }

后:

    [HttpPost]
    public ActionResult Index(List<AccessoryVM> form)
    {

我检查了关键字和属性的双重用途,但没有看到任何冲突。

1 个答案:

答案 0 :(得分:0)

您可以在帖子功能中使用此功能

 [HttpPost]
 public ActionResult Index(List<AccessoryVM> form)
 {
    foreach (var AccessoryVM in model.AccessoryVMs)
    {
       //do stuff with each of these here ..
    }
  }

此致 Jasbeer Singh