2011-06-08 62 views
0

這裏ANS方案發送數據: 我已經安裝的情侶喜歡的ViewModels的:ASP .NET MVC3如何從局部視圖

public class ThreadEditorView 
{ 
    public int ForumID { get; set; } 
    public ThreadEditorPartialView ThreadEditor { get; set; } 
    public ThreadEditorSpecial ThreadSpecial { get; set; } 
} 

現在我已經和查看:

@using (Html.BeginForm("NewThread", "Thread", FormMethod.Post, 
    new {@enctype="multipart/form-data"})) { 

    @Html.ValidationSummary(true) 
    <fieldset> 
     @Html.Partial("_ThreadEditor", Model.ThreadEditor) 
     @Html.Partial("_SpecialProperties", Model.ThreadSpecial) 
     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 
} 

的問題是如何我是否將部分視圖的數據傳遞給控制器​​? 我知道我可以簡單地這樣做:

public ActionResult NewThread(ThreadEditorView modelEditor, 
    ThreadEditorPartialView blabla, ThreadEditorSpecial zumg) 

但是,這不看真的很方便,我想在ThreadEditorView通過一切。

更新: SpecialView

@model Vaniv.Core.ViewModel.Forums.ThreadEditorSpecial 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.IsSticky) 
     </div> 
     <div class="editor-field"> 
      @Html.RadioButtonFor(model => model.IsSticky, false) 
      @Html.ValidationMessageFor(model => model.IsSticky) 
     </div> 
(some irrevalnt forms) 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.IsLocked) 
      @Html.ValidationMessageFor(model => model.IsLocked) 
     </div> 

和編輯:

@model Vaniv.Core.ViewModel.Forums.ThreadEditorPartialView 


    <legend>ThreadEditorPartialView</legend> 
    @Html.HiddenFor(model => model.ForumID) 
    <div class="editor-label"> 
     @Html.LabelFor(model => model.ThreadName) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.ThreadName) 
     @Html.ValidationMessageFor(model => model.ThreadName) 
    </div> 

(某些形式,是irrevelant)

</div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Message) 
    </div> 
    <div class="editor-field"> 
     @Html.TextAreaFor(model => model.Message) 
     @Html.ValidationMessageFor(model => model.Message) 
    </div> 
+0

我想的是,「不相關領域」可能不是無關緊要......都是這些字段不是「ThreadEditorPartialView」的一部分?你有沒有嘗試從部分中刪除它們,看它是否有效? – 2011-06-08 15:30:54

+0

那些「無用的字段」只是由腳手架生成的,看起來像我發佈的那些。我無法刪除它們,因爲我需要所有這些輸入數據。 – 2011-06-08 15:35:01

回答

1

我會使用編輯模板,而不是泛音建議你,因爲他們將生成的輸入字段適當的名稱,以便模型綁定能夠正確地解決POST操作價值的護理:

@using (Html.BeginForm("NewThread", "Thread", FormMethod.Post, 
    new {@enctype="multipart/form-data"})) { 

    @Html.ValidationSummary(true) 
    <fieldset> 
     @Html.EditorFor(x => x.ThreadEditor) 
     @Html.EditorFor(x => x.ThreadSpecial) 
     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 
} 

,並有相應的編輯器模板(注意,編輯模板的名稱和位置很重要):

~/Views/Shared/EditorTemplates/ThreadEditorPartialView.cshtml

@model ThreadEditorPartialView 
<legend>ThreadEditorPartialView</legend> 
@Html.HiddenFor(model => model.ForumID) 
<div class="editor-label"> 
    @Html.LabelFor(model => model.ThreadName) 
</div> 
<div class="editor-field"> 
    @Html.EditorFor(model => model.ThreadName) 
    @Html.ValidationMessageFor(model => model.ThreadName) 
</div> 

~/Views/Shared/EditorTemplates/ThreadEditorSpecial.cshtml

@model ThreadEditorSpecial 
... 

現在你的控制器動作可以簡單地是這樣的:

public ActionResult NewThread(ThreadEditorView modelEditor) 
{ 
    ... 
} 
+0

工作。尼斯。不知道EditorTemplates。 – 2011-06-08 15:40:48

0

什麼_ThreadEditor_SpecialProperties樣子?

如果諧音被輸入到ThreadEditorPartialViewThreadEditorSpecial分別ASP.NET MVC將呈現與幫助它的模型結合適當的POST名稱文本框:

_ThreadEditor:

@model ThreadEditorPartialView 
// rest of the view here 

_SpecialProperties:

@model ThreadEditorSpecial 
// rest of the view here 

對於正確輸入的部分,您的操作只需要將o NE參數:

public ActionResult NewThread(ThreadEditorView modelEditor) { } 
+0

這正是他們如何打字。但它似乎並不奏效。 – 2011-06-08 15:13:40

+0

@Łukasz - 你可以發佈至少2個部分中的一個的代碼? – 2011-06-08 15:14:33