gpt4 book ai didi

c# - 无法将 List 发布到 Controller

转载 作者:太空宇宙 更新时间:2023-11-03 21:14:24 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com