【发布时间】:2013-11-26 22:36:54
【问题描述】:
我正在努力创建正确的 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
});
但是,我每次发布这两个属性的尝试都会导致模型为空。我已经尝试过 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 考虑在内的新视图。
【问题讨论】:
-
你能把你的
View也发一下吗? -
嗯,当然!我会将其编辑到原始帖子中。
-
@AlexFilipovici 为什么需要
View代码?此处描述的问题仅与HTTP POST和Controller有关。
标签: c# asp.net-mvc jquery asp.net-mvc-routing