2013-05-17 46 views
1

我有以下模型。獲取額外的輸入名稱不在模型類別中

public class M 
{ 
    public int A { get; set; } 
    public int B { get; set; } 
} 

而且我有以下剃刀視圖。

@model MyApp.Models.M 
@using (Html.BEginForm("Create", "Test", new { id = ...}, FormMethod.Post)) { 
    @Html.EditorFor(m => m.A) 
    @Html.EditorFor(m => m.B) 
    <input name="C"> 
} 

控制器方法,

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create(Guid id, M m) 
{ 
    // How to get C? 
} 

如何獲得輸入C在控制器中的值? (一個解決方案可能是創建一個額外的視圖模型並將其映射模型,有沒有簡單的方法?)

回答

2

您可以添加一個名爲FormCollection的FormCollection的另一種方法的參數,你可以詢問這個對的FormCollection「C」 例如

var yourValueForC = formCollection["C"] 

或者您也可以查詢請求如下

var yourValueForC = Request.Form["C"] 
+0

這是正確的嗎? '公共ActionResult創建(Guid id,M m,字符串C)'? – ca9163d9

+0

如果你想要,你也可以這樣做。 –