2016-02-19 31 views
0

情景:
首先,對不起我的英語。 我試圖做的是發佈形式低谷-POST下列對象:用剃刀在MVC中發佈複雜對象列表

public class AppConfigViewModelInput 
{ 
    public string Setting { get; set; } 
    public string Value { get; set; } 
} 

以下方法:

[HttpPost] 
    public ActionResult Index(List<AppConfigViewModelInput> listOfAppConfigToUpdate) 
    { ... } 

但這輸入對象僅由兩個屬性構成視圖對象,我用它來顯示我的剃鬚刀頁上的數據:

public class AppConfigViewModel : AppConfigViewModelInput 
{ 
    public string Description { get; set; } 
    public string ConfigType { get; set; } 
    public int ViewOrderInWebAdmin { get; set; } 
    public string ViewSpecialBackgroundColor { get; set; } 
} 

我讀了很多問題和博客(檢查出的問題,以便參考)。最後我能得到我的剃鬚刀頁面下面的代碼(我只發佈表單代碼段):

@model List<PGWebAdmin.Models.AppConfigViewModel> 
@{ 
    var itemCnt = 0; 
} 
@foreach (var item in Model) 
    { 
     itemCnt++; 
     <input type="hidden" name="AppConfigViewModelInput.Index" value="@itemCnt" /> 
     <input type="text" class="input-sm form-control" value="@item.Value" name="AppConfigViewModelInput[@itemCnt].Value"/> 
     <input type="text" name="AppConfigViewModelInput[@itemCnt].Setting" value="@item.Setting"/> 
    } 

,並通過創建表單:

@using (Html.BeginForm("Index", "AppConfig", 
    FormMethod.Post, new { @class = "navbar-form navbar-right", role = "search" })) 
    { 

問題:
我可以發送數據,我正在用開發工具檢查以下信息:enter image description here

即發佈到該方法,並且該方法被擊中,但該參數的值爲空:

enter image description here

我測試和修正,嘗試了幾種方法可以做到這一點,但這是很遠,我可以拿到了,我不明白髮生了什麼。

我做錯了什麼?爲什麼我仍然無效?

任何幫助將被優先。 謝謝!

參考:
MVC post a list of complex objects
How can I post a list of items in MVC
Posting to a list<modeltype> MVC3

+0

生成你視圖中正確使用'for'環或'EditorTemplate'按照[此答案](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) –

回答

4

您需要更改參數的名稱以匹配您在「名稱」字段中發送的內容。

即更改後的控制器:

[HttpPost] 
public ActionResult Index(List<AppConfigViewModelInput> AppConfigViewModelInput) 
    { ... } 
+0

這也是。我的帖子在下面的作品,但這是重要的要記住。您將輸入名稱作爲對象名稱發佈。您需要將輸入名稱作爲您告訴活頁夾預期的名稱。 (不是型號) –

+0

這個解決了!非常感謝!!! –

1

我沒有看到你發送的AppConfigModelInput.Index的原因,我認爲這可能是你的問題。您發送的消息不應包含不屬於型號的數據

1

試試這個嗎?這可能不是最好的答案,但它應該適用於你的目的。

[HttpPost] 
    public ActionResult Index(AppConfigViewModelInput[] listOfAppConfigToUpdate) 
    { ... } 

而像這樣的HTML ..

@model List<PGWebAdmin.Models.AppConfigViewModel> 
@{ 
    var itemCnt = 0; 
} 
@foreach (var item in Model) 
    { 
     itemCnt++; 
     <input type="text" class="input-sm form-control" value="@item.Value" name="listOfAppConfigToUpdate[@itemCnt].Value"/> 
     <input type="text" name="listOfAppConfigToUpdate[@itemCnt].Setting" value="@item.Setting"/> 
    } 

我刪除指數的頂部輸入..我看不出它的用武之地。您可以將數組轉換成內部列表中指數法。

1

輸入文件名是錯誤的!由於您的HttpPost操作期望收集AppConfigViewModel。你並不需要AppConfigViewModelInput。輸入字段名稱的前綴。對於模型綁定工作,你輸入的字段名稱應該像

<input type="hidden" name="[0].Index" value="" /> 
<input type="hidden" name="[1].Index" value=" /> 

另外,還要確保你的表單元素是一個表單標籤。

以下應該工作。

@model List<PGWebAdmin.Models.AppConfigViewModel> 
@{ 
    var itemCnt = 0; 
} 
@using (Html.BeginForm()) 
{ 
    foreach (var item in Model) 
    { 

    <input type="hidden" name="[@itemCnt].Index" value="@itemCnt" /> 
    <input type="text" value="@item.Value" name="AppConfigViewModelInput[@itemCnt].Value"/> 
    <input type="text" name="[@itemCnt].Setting" value="@item.Setting"/> 
    itemCnt++; 
    } 
    <input type="submit" value="Save" class="btn btn-default" /> 
}