2013-11-26 61 views
0

我正在努力創建適當的AJAX請求來發布我的表單數據加上一個額外的數組到MVC控制器。POST模型加上ASP.NET 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考慮在內的新視圖。

+0

你還可以發佈你的'視圖'? –

+0

呃,當然!我會將其編輯到原始文章中。 –

+0

@AlexFilipovici爲什麼需要'View'代碼?這裏描述的問題只能通過'HTTP POST'和'Controller'來完成。 – Shiva

回答

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) 
    }); 
相關問題