ASP .NET MVC3如何从局部视图发送数据 [英] ASP .NET MVC3 how to send data from partial View

查看:96
本文介绍了ASP .NET MVC3如何从局部视图发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面ANS方案:
我有这样的设置对夫妇的ViewModels的:

 公共类ThreadEditorView
{
    公众诠释ForumID {搞定;组; }
    公共ThreadEditorPartialView ThreadEditor {搞定;组; }
    公共ThreadEditorSpecial ThreadSpecial {搞定;组; }
}

现在我已经和查看:

  @using(Html.BeginForm(NewThread,线程,FormMethod.Post,
    新{@ ENCTYPE =的multipart / form-data的})){    @ Html.ValidationSummary(真)
    <&字段集GT;
        @ Html.Partial(_ ThreadEditor,Model.ThreadEditor)
        @ Html.Partial(_ SpecialProperties,Model.ThreadSpecial)
        &所述p为H.;
            <输入类型=提交值=创建/>
        &所述; / P>
    < /字段集>
}

问题是我如何通过从局部视图中的数据到控制器?
我知道我可以简单地做到这一点:

 公众的ActionResult NewThread(ThreadEditorView modelEditor,
    ThreadEditorPartialView布拉布拉,ThreadEditorSpecial zumg)

但是,这并不看起来真的很方便,我想在ThreadEditorView通过一切。

更新:
SpecialView

  @model Vaniv.Core.ViewModel.Forums.ThreadEditorSpecial        < D​​IV CLASS =编辑标记>
            @ Html.LabelFor(型号=> model.IsSticky)
        < / DIV>
        < D​​IV CLASS =主编场>
            @ Html.RadioButtonFor(型号=> model.IsSticky,FALSE)
            @ Html.ValidationMessageFor(型号=> model.IsSticky)
        < / DIV>
(有些irrevalnt形式)
        < D​​IV CLASS =主编场>
            @ Html.EditorFor(型号=> model.IsLocked)
            @ Html.ValidationMessageFor(型号=> model.IsLocked)
        < / DIV>

和编辑器:

  @model Vaniv.Core.ViewModel.Forums.ThreadEditorPartialView
    <传奇> ThreadEditorPartialView< /传说>
    @ Html.HiddenFor(型号=> model.ForumID)
    < D​​IV CLASS =编辑标记>
        @ Html.LabelFor(型号=> model.ThreadName)
    < / DIV>
    < D​​IV CLASS =主编场>
        @ Html.EditorFor(型号=> model.ThreadName)
        @ Html.ValidationMessageFor(型号=> model.ThreadName)
    < / DIV>

(一些形式中,是irrevelant)

 < / DIV>    < D​​IV CLASS =编辑标记>
        @ Html.LabelFor(型号=> model.Message)
    < / DIV>
    < D​​IV CLASS =主编场>
        @ Html.TextAreaFor(型号=> model.Message)
        @ Html.ValidationMessageFor(型号=> model.Message)
    < / DIV>


解决方案

我会建议你使用编辑器的模板,而不是泛音,因为他们将采取您的输入域产生适当的名称照顾,使模型绑定能够正确地解析在POST操作的值:

  @using(Html.BeginForm(NewThread,线程,FormMethod.Post,
    新{@ ENCTYPE =的multipart / form-data的})){    @ Html.ValidationSummary(真)
    <&字段集GT;
        @ Html.EditorFor(X => x.ThreadEditor)
        @ Html.EditorFor(X => x.ThreadSpecial)
        &所述p为H.;
            <输入类型=提交值=创建/>
        &所述; / P>
    < /字段集>
}

和具备相应的编辑模板(注意,编辑模板的名称和位置是重要):

〜/查看/共享/ EditorTemplates / ThreadEditorPartialView.cshtml

  @model ThreadEditorPartialView
<传奇> ThreadEditorPartialView< /传说>
@ Html.HiddenFor(型号=> model.ForumID)
< D​​IV CLASS =编辑标记>
    @ Html.LabelFor(型号=> model.ThreadName)
< / DIV>
< D​​IV CLASS =主编场>
    @ Html.EditorFor(型号=> model.ThreadName)
    @ Html.ValidationMessageFor(型号=> model.ThreadName)
< / DIV>

〜/查看/共享/ EditorTemplates / ThreadEditorSpecial.cshtml

  @model ThreadEditorSpecial
...

现在您的控制器动作可能只是看起来像这样:

 公众的ActionResult NewThread(ThreadEditorView modelEditor)
{
    ...
}

Here ans Scenario: I have setup couple of ViewModels like:

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

Now I have and View:

@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>
}

Question is how do I pass data from partial views into controller ? I know I can simply do this:

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

But that doesn't look really convenient, I'd like to pass everything in ThreadEditorView.

Update: 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>

And Editor:

@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>

(some forms, that are 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>

解决方案

I would recommend you using editor templates instead of partials as they will take care of generating proper names for your input fields so that the model binder is able to correctly resolve the values in the POST action:

@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>
}

and have the corresponding editor templates (note that the names and location of the editor templates are important):

~/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>

and ~/Views/Shared/EditorTemplates/ThreadEditorSpecial.cshtml:

@model ThreadEditorSpecial
...

Now your controller action could simply look like this:

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

这篇关于ASP .NET MVC3如何从局部视图发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆