我在视图中添加了一个按钮.单击此按钮时,将添加部分视图.在我的表单中,我可以添加尽可能多的局部视图.提交此表单数据时,我无法将所有部分视图数据发送到控制器.我创建了一个具有所有属性的不同模型,并且我已经将该模型的列表添加到我的主模型中.任何人都可以给我一些技巧,以便我可以将所有部分视图内容发送到我的控制器?
在我看来
<div id="CSQGroup">
</div>
<div>
<input type="button" value="Add Field" id="addField" onclick="addFieldss()" />
</div>
function addFieldss()
{
$.ajax({
url: '@Url.Content("~/AdminProduct/GetColorSizeQty")',
type: 'GET',
success:function(result) {
var newDiv = $(document.createElement("div")).attr("id", 'CSQ' + myCounter);
newDiv.html(result);
newDiv.appendTo("#CSQGroup");
myCounter++;
},
error: function(result) {
alert("Failure");
}
});
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中
public ActionResult GetColorSizeQty()
{
var data = new AdminProductDetailModel();
data.colorList = commonCore.getallTypeofList("color");
data.sizeList = commonCore.getallTypeofList("size");
return PartialView(data);
}
[HttpPost]
public ActionResult AddDetail(AdminProductDetailModel model)
{
....
}
Run Code Online (Sandbox Code Playgroud)
在我的部分视图中
@model IKLE.Model.ProductModel.AdminProductDetailModel
<div class="editor-field">
@Html.LabelFor(model => model.fkConfigChoiceCategorySizeId)
@Html.DropDownListFor(model => model.fkConfigChoiceCategorySizeId, …Run Code Online (Sandbox Code Playgroud) 我做了一个小项目来理解Stephen Muecke的答案:将多次调用的同一部分视图提交给控制器?
几乎一切都有效.javascript在Partial View中添加了新的字段,我可以通过控制器方法为局部视图插入的"temp"值来告诉他们它们与模型绑定.
但是,当我提交新字段时,AddRecord()方法抛出一个异常,表明模型没有被传入("对象引用未设置为对象的实例").
此外,当我查看页面源时,BeginCollectionItem帮助器正在主视图中的表周围插入一个隐藏标记,该标记显示预先存在的记录,但不包括javascript添加的新字段.
我究竟做错了什么?我很擅长这一点,谢谢你的耐心等待!
我的主要观点:
@model IEnumerable<DynamicForm.Models.CashRecipient>
@using (Html.BeginForm("AddDetail", "CashRecipients", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div id="CSQGroup">
</div>
}
<div>
<input type="button" value="Add Field" id="addField" onclick="addFieldss()" />
</div>
<script>
function addFieldss()
{
//alert("ajax call");
$.ajax({
url: '@Url.Content("~/CashRecipients/RecipientForm")',
type: 'GET',
success:function(result) {
//alert("Success");
var newDiv = document.createElement("div");
var newContent = document.createTextNode("Hi there and greetings!");
newDiv.appendChild(newContent);
newDiv.innerHTML = result;
var currentDiv = document.getElementById("div1");
document.getElementById("CSQGroup").appendChild(newDiv);
},
error: function(result) {
alert("Failure");
}
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
我的部分观点:
@model DynamicForm.Models.CashRecipient
@using HtmlHelpers.BeginCollectionItem
@using (Html.BeginCollectionItem("recipients")) …Run Code Online (Sandbox Code Playgroud) javascript asp.net-mvc model-binding asp.net-mvc-partialview begincollectionitem