【发布时间】:2021-11-30 01:28:11
【问题描述】:
我正在尝试向旧的 ASP.NET MVC 项目添加新功能。每次点击按钮都想添加新的地址表单,而且客户的地址是列表,会动态添加,问题是模型在发布时没有绑定地址列表数据。
这是我Customer的模型:
public class CustomerInfo
{
public CustomerModel CUSTOMER { get; set; }
public CustomerAddress CustomerAddresses { get; set; }
}
地址模型:
public class CustomerAddresses
{
public string Address { get; set; }
}
这是附加地址列表的 jQuery 代码:
$(document).on('click', "#addAddressItem", function (e) {
e.preventDefault();
$.ajax({
url: this.href,
cache: false,
success: function (html) {
$("#RegisterAddressId").append(html);
}
});
});
加载部分视图的操作链接:
<div>
<div class="address">
</div>
<p>
@Html.ActionLink("add address", MVC.Applications.RegisterAddress(), new
{ id = "addAddressItem" })
</p>
<div id="RegisterAddressId">
</div>
</div>
部分视图操作:
public virtual PartialViewResult RegisterAddress()
{
return PartialView(MVC.Applications.Views.CustomerAddress);
}
局部视图:
@model WeaponPermissions.Models.RegisterCustomerAddress
<table>
<td class="personlistborder">
<div class="editor-label">
Address
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address, new { @class = "CustomerAddressField" })
@Html.ValidationMessageFor(model => model.Address)
</div>
</td>
<td colspan="7"></td>
<td>
<span id="deleteRegisterCustomerAddress" class="outline ui-button ui-widget ui-state-
default ui-corner-all" role="button" aria-disabled="false">
<span class="ui-icon-minusthick ui-icon "></span>
</span>
</td>
</table>
如有必要,我还应该删除附加的地址字段,这就是为什么有一个 ID 为 deleteRegisterCustomerAddress 的跨度
jQuery 将数据发布到点击操作:
buttons: ({
"Save": {
text: "save",
id: "btnSubmitExamApp",
click: function () {
var form = $('form', this);
var validator = $(form).validate();
if (validator.form()) {
debugger
var res = JSON.parse(form);
res.submit();
}
$(this).submit();
}
},
我做错了什么?研究这个话题很久了,没找到解决办法
【问题讨论】:
标签: c# jquery asp.net-mvc razor partial-views