【问题标题】:POST model plus additional array to ASP.NET MVC controllerPOST 模型和附加数组到 ASP.NET MVC 控制器
【发布时间】: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 POSTController 有关。

标签: c# asp.net-mvc jquery asp.net-mvc-routing


【解决方案1】:

创建一个包含域模型和数组的视图模型。您还可以使用 AJAX form instead of an Html form 让您的生活更轻松,这样 MVC 框架将为您处理 POST。

【讨论】:

    【解决方案2】:

    试试这个。它对我有用。

    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)
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-25
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      • 2018-05-02
      • 2011-05-23
      • 1970-01-01
      相关资源
      最近更新 更多