我正在努力创建适当的AJAX请求,以便将我的表单数据和一个额外的数组发布到MVC控制器。
[HttpPost, AjaxOnly]
public ActionResult GetAttributeViews(AddComponentsDialogModel model, List<int> attributeIDs)
这是我试图点击的方法签名。
// Post the model to GetAttributeViews:
$.ajax({
type: 'POST',
url: '../Component/GetAttributeViews',
data: $('form').serialize()
});
// Post the integer array to GetAttributeViews:
$.ajax({
type: 'POST',
url: '../Component/GetAttributeViews',
data: {
attributeIDs: [1, 2, 3]
},
traditional: true
});
但是,我发布这两个属性的每次尝试都会导致模型为null。我已经尝试过JSON.stringify,$ .param({},true)以及设置AJAX请求的dataType和contentType选项。这似乎都没有帮助。
将这两个值发布到我的控制器并让MVC模型绑定器翻译我的模型的正确方法是什么?
以下是有问题的观点:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CableSolve.Web.Models.Component.AddComponentsDialogModel>" %>
<% Html.BeginForm(); %>
<div class="editableContentWrapper twoColumnLayout">
<fieldset class="collapsible" id="<%= Model.AddComponentsLegendName.Replace(" ", "_") + "_Fieldset" %>">
<legend>
<%= Html.DisplayFor(model => model.AddComponentsLegendName)%>
</legend>
<div class="collapsibleDiv">
<div class="detailsRow required">
<%= Html.LabelFor(model => model.NameStart, Model.NameStartLabel)%>
<%= Html.EditorFor(model => model.NameStart) %>
</div>
<div class="detailsRow">
<%= Html.LabelFor(model => model.NameEnd, Model.NameEndLabel)%>
<%= Html.EditorFor(model => model.NameEnd)%>
</div>
<div class="detailsRow required">
<%= Html.LabelFor(model => model.RequiredSpaceLookup, Model.RequiredSpaceLookupLabel)%>
<%= Html.EditorFor(model => model.RequiredSpaceLookup)%>
</div>
<div class="detailsRow required">
<%= Html.LabelFor(model => model.RequiredTemplateLookup, Model.RequiredTemplateLookupLabel)%>
<%= Html.EditorFor(model => model.RequiredTemplateLookup)%>
</div>
<div class="detailsRow">
<%= Html.LabelFor(model => model.BarcodeStart, Model.BarcodeStartLabel)%>
<%= Html.EditorFor(model => model.BarcodeStart)%>
</div>
<div class="detailsRow">
<%= Html.LabelFor(model => model.BarcodeEnd, Model.BarcodeEndLabel)%>
<%= Html.EditorFor(model => model.BarcodeEnd)%>
</div>
</div>
</fieldset>
</div>
<div class="editableContentWrapper twoColumnLayout">
<fieldset class="collapsible" id="<%= Model.AttributesLegendName.Replace(" ", "_") + "_Fieldset" %>">
<legend>
<%= Html.DisplayFor(model => model.AttributesLegendName) %>
</legend>
<div class="collapsibleDiv attributeControlArea">
<%= Html.EditorFor(model => model.Attributes) %>
</div>
</fieldset>
</div>
<% Html.EndForm(); %>
<button type="button" class="addAttributes">Add Attributes</button>
<button type="button" class="clearAttributes">Clear Attributes</button>
<div id="AddAttributesLookup"></div>
这个想法是在初始运行时属性将为空,但客户端可以选择一些我想要加载视图的属性ID,同时还要记住所有当前数据。所以,我想将我的模型的当前状态以及属性ID列表传递回我的控制器,以便它可以创建一个新视图来考虑这些ID。
答案 0 :(得分:1)
创建一个包含域模型和数组的视图模型。您也可以使用AJAX form instead of an Html form让您的生活更轻松,这样MVC框架将为您处理POST。
答案 1 :(得分:0)
试试吧。它对我有用。
var data = new Object();
data = {
model: { ID: "1", Name: "Test" }, // <<-- This is temporary AddComponentsDialogModel object
attributeIDs: [1, 2, 3]
};
$.ajax({
type: 'POST',
url: '@Url.Content("~/Home/GetAttributeViews")',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(data)
});