2013-03-11 88 views
2

我試圖創建一個單一的視圖,允許用戶使用索引視圖模型查看當前列出的項目,然後還允許用戶使用單獨的創建視圖模型創建一個新項目部分查看提交返回null

所以我有兩個的ViewModels -IndexFunkyThingsViewModel -CreateFunkyThingViewModel

在本質上我已經有了一個主視圖:

@model IndexFunkyThingsViewModel 
@foreach (var item in Model.FunkyThings) 
{ 
/*Indexy stuff*/  
} 

/*If create item*/ 
@if (Model.CreateFunkyThing) 
{ 
@Html.Partial("_CreateFunkyThingPartial", new CreateFunkyThingViewModel()); 
} 

然後在我的部分觀點我有

@model CreateFunkyThingViewModel 
@using (Html.BeginForm(MVC.FunkyThings.CreateFunkyThing(Model))) 
{ 
    @Html.ValidationSummary(true) 
    <fieldset> 
     <legend>Create FunkyThing</legend> 
     @Html.EditorForModel(); 
     <p> 
      <input type="submit" class="button green" value="CreateFunkyThing" /> 
     </p> 
    </fieldset> 
} 

最後,在控制我有:

[HttpPost] 
public virtual ActionResult CreateFunkyThing(CreateFunkyThingViewModel createFunkyThingViewModel) 
{ 
    .. 
} 

這一切似乎都愉快地編譯和當我去到視圖它迄今爲顯示領域的創建以及諸如此類的作品。但是,當我點擊提交按鈕時,控制器不會收到任何數據。但是,在調試器中調用ActionResult時,如果submit按鈕調用createFunkyThingViewModel參數,則該參數爲null。

我在做什麼錯?

回答

2

發佈到您的控制器時,您不會將模型發送給它。使用這個:

@using (Html.BeginForm("CreateFunkyThing", "ControllerName", FormMethod.Post)) 

然後刪除按鈕周圍的p標籤,並且不要使用任何東西。

段落標籤傾向於將按鈕與表單分開分組,即使它們位於相同的外部容器中。

+0

非常感謝! – 2013-03-11 15:04:22