无法将List <t>发布到控制器

时间:2016-02-16 09:28:31

标签: c# asp.net-mvc forms http-post partial-views

我有view使用以下代码动态呈现Partial View

主视图

@model JoyRydeStoreWebPortal.Areas.Admin.Models.StoreDetailModel
@{
 ViewBag.Title = "StoreDetail";
 Layout = "~/Areas/Admin/Views/Shared/_layoutAdmin.cshtml";
}

 <div class="panel">
        <div class="panel-heading">
            <h3 class="panel-title">Store category</h3>
            <div class="panel-actions">
                <a href="#editCategory" id="createCategory" data-toggle="modal" class="btn btn-default"><i class="fa fa-plus" aria-hidden="true"></i></a>
            </div>
        </div>

        <div class="panel-body">

            <ul class="list-group list-group-dividered list-group-full">
                @{foreach (var cat in Model.Categories){
                      if (cat.IsExist) { 
                        <li class="list-group-item">@cat.CategoryName</li>
                      }
                  }  
                }                    
            </ul>

        </div>

    </div>

 // Render Partial View and open in bootstrap modal

 $("#createCategory").on("click", function () {

    $.ajax({
        contentType: "application/json; charset=utf-8",
        url: "../displayCategories/" + GetURLParameter(),
        success: function (partialViewResult) {
            $("#catModalBox").html(partialViewResult);
            $("#editCategory").modal();


            $('#editCategory .close-btn').on("click", function () {


                $('#editCategory').hide('slow');
                $('.modal-backdrop').hide('slow');
            });
        },
        error: function (xhr, ajaxOptions, thrownError) {
            debugger;
        }

    });
});

控制器

 [HttpGet]
    public ActionResult displayCategories(string info)
    {
        var StoreID = Convert.ToInt64(Request.RequestContext.RouteData.Values["id"]);
        return PartialView("Partial_CreateCategory", getStoreCategories(StoreID));
    }
[HttpPost]
    public ActionResult SaveCategories(List<StoreCategoryDetail> lstCat)
    {
       // Getting lstCat Null here
        return RedirectToAction("StoreDetails");
    }

部分视图

@model List<JoyRydeStoreWebPortal.Areas.Admin.Models.StoreCategoryDetail>

 <div class="modal fade" id="editCategory" aria-hidden="false" role="dialog" tabindex="-1" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog">
    <form class="modal-content" method="post" action="@Url.Action("SaveCategories","Index", new{ Area = "Admin" })">         
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">×</span>
            </button>
            <h4 class="modal-title" id="createStoreTitle">Edit Category</h4>
        </div>
        <div class="modal-body">
            <div class="">                    
                @for (int i = 0; i < Model.Count; i++)
                {
                    <div class="checkbox-custom checkbox-default">
                        @Html.CheckBoxFor(m => m.ElementAt(i).IsExist, new { @id = "chk" + i })
                        @Html.Label("chk" + i, Model.ElementAt(i).CategoryName)
                    </div>
                }
            </div>
        </div>
        <div class="modal-footer">
            <button type="button"  class="btn btn-default close-btn">Close</button>
            <button type="submit" class="btn btn-primary">Save changes</button>
        </div>
    </form>
</div>

在表单上提交Partial View,我在Null中获得List<StoreCategoryDetail>。为什么?

2 个答案:

答案 0 :(得分:4)

使用@Html.CheckBoxFor(m => m.ElementAt(i).IsExist)生成没有索引器的名称属性,即

<input type="checkbox" name="IsExist" .... />
<input type="hidden" name="IsExist" .... />

您需要使用@Html.CheckBoxFor(m => m[i].IsExist),这将生成正确的名称属性

<input type="checkbox" name="[0].IsExist" .... />
<input type="hidden" name="[0].IsExist" .... />

请注意,无需覆盖默认的id属性,并且要使标签生效,您可以使用

@Html.LabelFor(m => m[i].IsExist, Model[i].CategoryName)

答案 1 :(得分:0)

@Html.CheckBoxFor(m => m[i].IsExist, new { @id = "chk" + i })