从 ajax jQuery 调用传递空的 mvc 模型
本文关键字:mvc 模型 ajax jQuery 调用 | 更新日期: 2023-09-27 18:36:14
>我有一个返回地址输入组的部分视图,一个是加拿大的,一个是美国人。我的部分使用模型来编辑当前数据,但它也处理添加新数据。因此,模型应为空。到目前为止,我无法加载部分。
jQuery中的Ajax调用:
$.ajax({
url: 'Views/AddressByCountry',
type: 'GET',
async: false,
data: { model: null, country: country },
contentType: 'application/json; charset=utf-8',
success: function(page) {
alert(page);
$('.addressarea').html(page);
}
});
调用部分的主视图:
@if (@Model == null) //addnew
{
<div class="col-md-12">
<div class="row">
<div class="col-md-5 ">
@Html.Partial("AddressByCountry", Model)
</div>
<div class="col-md-7 addressarea">
<input id="CountryChkBox" type="checkbox" name="@country">
<label id="countryLabel" class="no-colon">@((country.ToString()) == "US" ? "Canadian Address" : "USA Address")</label>
</div>
</div>
</div>
}
部分视图:
@model MyModel.Contact
@{
Layout = null;
var country = TempData["country"];
}
@if (country.ToString() == "US")
{
<div class="col-md-12">
<div class="form-inline">
<div class="form-group">
@Html.Label("Address", null, new { @class = "control-label no_wrap" })
@Html.TextBox("Address", null, new { @class = "form-control ", @onKeyup = "enableButton()", tabindex = "4" })
</div>
<div class="form-group">
@Html.Label("Address2", null, new { @class = "control-label no_wrap" })
@Html.TextBox("Address2", null, new { @class = " form-control ", @onKeyup = "enableButton()", tabindex = "4" })
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-inline">
<div class="form-group">
@Html.Label("City", null, new { @class = "control-label no_wrap" })
@Html.TextBox("City", null, new { @class = "form-control ", @onKeyup = "enableButton()", tabindex = "4" })
</div>
<div class="form-group">
@Html.Label("State", null, new { @class = "control-label no_wrap" })
@Html.TextBox("State", null, new { @class = "form-control ", @onKeyup = "enableButton()", tabindex = "4" })
</div>
<div class="form-group">
@Html.Label("Zip", null, new { @class = "control-label no_wrap" })
@Html.TextBox("Zip", null, new { @class = "form-control ", @onKeyup = "enableButton()", tabindex = "4" })
</div>
</div>
</div>
}
else
{
<div class="col-md-12">
<div class="form-inline">
<div class="form-group">
@Html.Label("Address", null, new { @class = "control-label no_wrap" })
@Html.TextBox("Address", null, new { @class = "form-control ", @onKeyup = "enableButton()", tabindex = "4" })
</div>
<div class="form-group">
@Html.Label("Address2", null, new { @class = "control-label no_wrap" })
@Html.TextBox("Address2", null, new { @class = " form-control ", @onKeyup = "enableButton()", tabindex = "4" })
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-inline">
<div class="form-group">
@Html.Label("City", null, new { @class = "control-label no_wrap" })
@Html.TextBox("City", null, new { @class = "form-control ", @onKeyup = "enableButton()", tabindex = "4" })
</div>
<div class="form-group">
@Html.Label("Province", null, new { @class = "control-label no_wrap" })
@Html.TextBox("Province", null, new { @class = "form-control ", @onKeyup = "enableButton()", tabindex = "4" })
</div>
<div class="form-group">
@Html.Label("Zip", null, new { @class = "control-label no_wrap" })
@Html.TextBox("Zip", null, new { @class = "form-control ", @onKeyup = "enableButton()", tabindex = "4" })
</div>
</div>
</div>
}
控制器:
public ActionResult AddressByCountry(MyModel.Contact model, string country = "US")
{
TempData["country"] = country;
return PartialView("AddressByCountry", model);
}
联系人类:
public class Contact
{
public bool IsActive { get; set; }
public int Id { get; set; }
public string FullName
{
get
{
if (FirstName != null && LastName != null)
{
return LastName + ", " + FirstName;
}
else
return string.Empty;
}
}
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Province { get; set; }
public string Zip { get; set; }
public string Category { get; set; }
public List<ContactList> Contacts { get; set; }
}
尝试像这样调用您的部分视图
@Html.Partial("AddressByCountry", Model == null ? new Contact() : Model)
要在 ajax 调用中传递此模型,
var getModelData = @Html.Raw(Json.Encode(Model));
$.ajax({
url: 'Views/AddressByCountry',
type: 'GET',
async: false,
data: { model: getModelData , country: country },
contentType: 'application/json; charset=utf-8',
success: function(page) {
alert(page);
$('.addressarea').html(page);
}
});