【发布时间】:2019-03-02 19:27:21
【问题描述】:
我有一个主视图和两个局部视图
- 索引 - 主视图
- _详细信息 - 部分视图
- _Address- 详细信息部分视图中的部分视图
索引 - 主视图
@model IndexViewModel
@{
Layout = "~/Views/Shared/_CodeLayout.cshtml";
}
@Html.Partial("_Details", Model)
2。 _Details - 局部视图
@model IndexViewModel
<div id="Detailsdiv">
@using (Html.BeginForm("_Details", "FS", FormMethod.Post, new
{
id =
"frmFS",
@class = "form-horizontal"
}))
{
<div class="row">
<div class="col-lg-1 nopadding">
<div class="col-lg-1 nopadding">
@Html.LabelFor(m => m.User.Name):
</div>
<div class="col-lg-2">
@Html.TextBoxFor(m => m.User.Name, new { @id = "txtName", @style = "width:140px;height:24px", @maxlength = "25" })
</div>
</div>
</div>
@Html.Action("_Address", "Shared", new { userId = Model.User.UserId })
}
<button type="button" value="Save" ID="btnSave"> Save </button>
}
</div>
3. _地址-部分视图
@model AddressViewModel
<div id="divAddress">
@using (Html.BeginForm("_Address", "Shared", FormMethod.Post, new { id = "frmAddress", @class = "form-horizontal" }))
{
<div class="row">
<div class="col-lg-1 nopadding">
@Html.LabelFor(m => m.Address.AddressDesc):
</div>
<div class="col-lg-2">
@Html.TextBoxFor(m => m.Address.AddressDesc, new { @id = "txtAName", @style = "width:140px;height:24px", @maxlength = "25" })
</div>
</div>
---
-
-
And some more
<button type="button" value="Save" ID="btnCreate"> Create </button>
</div>
}
$('#btnCreate').click(function () {
$("#ajax_loader").show();
var inputdata = $("#frmAddress").serialize();
$.ajax({
url: '@Url.Action("CreateAddress", "Shared")',
type: 'POST',
cache: false,
data: inputdata
}).done(function (result) {
$("#divAddress").html(result);
$("#ajax_loader").hide();
});
});
SharedConroller
获取地址操作
public ActionResult _ Address (short userId )
{
}
public ActionResult CreateAdderess(AddressViewModel addressViewModel)
{
Create address…………..
But AddressViewModel is coming null
}
}
public class AddressViewModel
{
[Key]
public decimal AddressId { get; set; }
[DisplayName("Address Type")]
public string Addr_Typ { get; set; }
[DisplayName("Address Description")]
[MaxLength(256)]
public string Addr_Desc { get; set; }
[DisplayName("City")]
[MaxLength(30)]
public string City_Nm { get; set; }
[DisplayName("State")]
[MaxLength(2)]
public string State_Cd { get; set; }
[DisplayName("Zip")]
[RegularExpression(@"^\d{5}(?:[\-]?\d{4})?$", ErrorMessage = "Invalid Zip")]
[MaxLength(10)]
public string Zip { get; set; }
}
从 _Details 回发工作正常。从地址回发 - 部分视图不起作用。
提前致谢
【问题讨论】:
-
首先,您的视图没有
id="frmAddress" - you have shown a form withid="frmFS". Second, you have not shown your models or what inputs you have in the_Address` 部分的元素。第三,脚本不会出现在部分中,它们会出现在主视图或其布局中。 -
抱歉弄错了。我已经更正了代码。
-
您还没有显示您的模型 - 但是假设您有公共属性,
addressViewModel的Address属性的代码将很好地绑定。但是你也有嵌套的表单,它是无效的 html。 -
是的,我有公共财产,更新了问题。所以我的局部视图中确实有多个按钮,例如创建、编辑和删除。我想在 Htm.Begin 表单中放置部分视图并处理 java 脚本中的每个按钮单击并执行 AJAX 发布。然后我在回发时将模型设为 NULL。服务器如何处理这些事件?
标签: asp.net-mvc-4 asp.net-mvc-5