2011-04-22 71 views
0

所以,我有一個操作方法是這樣的:MVC表單提交多個參數

public ViewResult CareerSearch() 
{ 
    CareerSearchModel model = GetCareerSearchModel(); 

    return View("Search", model); 
} 

在視圖中,我手動從模特屬性之一創建一個複選框列表。 ,它的輸出最終看起來是這樣的:

<input id="location51438342" type="checkbox" checked="True" value="2" name="locations"> 
<label for="location51438342">Austin</label> 
<input id="location14609737" type="checkbox" checked="True" value="9" name="locations"> 
<label for="location14609737">Dallas</label> 
<input id="location25198218" type="checkbox" checked="True" value="11" name="locations"> 
<label for="location25198218">Houston</label> 

因此,在處理表單POST操作方法,我想給模型和複選框整數數組的引用。但是,當我通過以下操作方法執行時,「模型」爲空:

[HttpPost] 
public ViewResult CareerSearch(CareerSearchModel model, int[] locations) 
{ 
    //omitted for brevity 
} 

我在這裏丟失了什麼?我如何獲得對我的模型和複選框值的數組的引用?

+0

模型是否有名爲「位置」的屬性,您試圖綁定?還是你指的是你在示例中沒有顯示的其他屬性? – Quesi 2011-04-22 18:57:12

回答

0

您必須將模型本身的所有字段發佈回來,也就是將它們隱藏在表單內的hiddenfor標籤中。你可以使用螢火蟲來查看回傳的內容。如果它沒有被髮回,你必須在帖子後的操作中重新創建它。

0

該模型未保存在服務器上的任何位置,並且不會自動回發到POST數據中(使用Fiddler進行確認)。您必須按照GET操作中的相同方式實例化模型,並使用其他參數相應地修改它。

請參閱NerdDinner瞭解如何將POST表單數據映射到您的模型中。