无法将列表发布到控制器
本文关键字:控制器 列表 | 更新日期: 2023-09-27 18:34:43
>我有一个使用以下代码动态呈现Partial View
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
,我在List<StoreCategoryDetail>
中得到了Null
。为什么?
您使用@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)
@Html.CheckBoxFor(m => m[i].IsExist, new { @id = "chk" + i })