c# - 无法将 List<T> 发布到 Controller

标签 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;
        }

    });
});

Controller

 [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");
    }

局部 View

@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 ,我得到 NullList<StoreCategoryDetail> .为什么?

最佳答案

您使用 @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)

关于c# - 无法将 List<T> 发布到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35428576/

相关文章:

c# - 显示表格的所有边框线

ruby-on-rails - 如何修复运行 Test::Unit 测试时产生的警告

jQuery基于Field的自动计算

jquery 在父容器上捕获模糊事件

C# 插入字符串到数据库问题

c# - wpf:usercontrol 与 customcontrol 性能问题

c# - 使用 File.Create 和 Delete 进行单元测试,但出现 "file used by other process"异常

sql-server - 在 App_Data 文件夹中使用 sql 数据库发布 mvc 网站

c# - ASP.NET C# MVC4 中的双尖括号

asp.net-mvc - 阻止对 Redis key 的访问 (ServiceStack)