我目前正在使用ASP.NET MVC 4.我正在尝试使用JQuery在某些情况下呈现页面的特定部分。为此,我编写了以下JQuery代码:
var selectedLicense = '@Model.License';
$.post("/Permission/LicenseConfigurationLabelOrCombobox", selectedLicense,
function (responseText) {
$(".LicenseWizard").html(responseText);
});
在我的控制器中,我有以下动作:
public String LicenseConfigurationLabelOrCombobox(LicenseWithCharacteristicsModel license)
{
//Do Stuff
}
我的动作模型仍然是空的。为了尝试新的东西,我在我的模型中做了以下几点:
public class PermissionLicenseConfigurationModel
{
public LicenseWithCharacteristicsModel License { get; set; }
public string JsonLicense
{
get
{
return new JavaScriptSerializer().Serialize(License);
}
}
}
我也更新了我的JQuery:
var selectedLicense = '@Model.JsonLicense';
$.post("/Permission/LicenseConfigurationLabelOrCombobox", selectedLicense,
function (responseText) {
$(".LicenseWizard").html(responseText);
});
我可以看到我的JQuery使用了一个真正的序列化对象,但是我的动作模型没有提取它。 Id始终为0,值为null。
任何提示?
答案 0 :(得分:1)
首先从视图模型中删除此JsonLicense
属性:
public class PermissionLicenseConfigurationModel
{
public LicenseWithCharacteristicsModel License { get; set; }
}
然后在视图中,您可以将模型作为JSON请求发送给控制器:
var selectedLicense = @Html.Raw(Json.Encode(Model.License));
$.ajax({
url: '@Url.Action("LicenseConfigurationLabelOrCombobox", "Permission")',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(selectedLicense),
success: function(result) {
$(".LicenseWizard").html(result);
}
});