2011-06-08 81 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