ASP.NET MVC3嵌套的局部视图? [英] ASP.NET MVC3 Nested Partial Views?

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

问题描述

我不知道是否有一个简单的方法来做到以下几点:

说我有一个博客,我想条目过去X个的主视图。

对于每个这些条目的我有标题的小模板,作者的头像和该职位的机构的名称。

很简单,我的模板,为博客条目,但如果我想通过注入另一种局部视图到哪里,我把身体后使用此模板用于多种用途?有一个简单的方法在MVC3做到这一点。请原谅我,如果这很简单,我可能会丢失一个搜索词:)

下面是一个例子:

模板为每一个入口

  @ {
    ViewBag.Title =_entry;
}<链接rel =stylesheet属性HREF =@ Url.Content(〜/ CSS / post.css)/><节ID =内容类=身体>
  < hgroup>
    < OL ID =帖子列表级=饲料>
      <立GT;
        <物品类=项>
          <报头GT;
            < H2类=条目标题>
                < A HREF =#相对=书签称号=永久链接这个帖子标题>
                &所述; / A>
            < / H>
          < /头>
          <页脚类=信息后>
            <简称类=出版的标题=2005-10-10T14:07:00-07:00>
                < - YYYYMMDDThh:MM:SS + ZZZZ - >
            < /缩写>
            <地址类=名片作者>
                <一类=URL FN的href =#>
                &所述; / A>
            < /地址>
          < /页脚>
        < /条>
      < /李>
      <立GT;
        <物品类=项>
          <页脚类=信息后>
            <地址类=名片作者>
                &所述; IMG SRC =>&下; / IMG>
            < /地址>
          < /页脚>
          &所述;! - /.post-info - >
          < D​​IV CLASS =条目内容>
            &所述p为H.;
            &所述; / P>
        < / DIV>
          &所述;! - /.entry-content - >
        < /条>
      < /李>
    < / OL>
  < / hgroup>
  <! - /#帖子列表 - >
< /节>

和它如何在不同的上下文中使用(除非这个重新$ P $一个例子psents期望结束前pression ......我不知道是否有一种方法采取的条目,作为视图和动态注入的局部视图进去(在下面的示例中,注入将是登录视图):

  @model MyTemplate.Models.LogOnModel@ {
    Page.Title =登录;
}@section HeadContent
{
    <链接rel =stylesheet属性HREF =@ Url.Content(〜/ CSS / openid.css)/>
    <链接rel =stylesheet属性HREF =@ Url.Content(〜/ CSS / post.css)/>
}
@section ScriptSection
{
    <脚本类型=文/ JavaScript的SRC =@ Url.Content(〜/ JS /库/ OpenID的jquery.js和)>< / SCRIPT>
    <脚本类型=文/ JavaScript的>
        $(文件)。就绪(函数(){
            openid.init('openid_identifier');
        });
    < / SCRIPT>
    <脚本SRC =htt​​p://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.min.js类型=文/ JavaScript的>< / SCRIPT>
    <脚本SRC =htt​​p://ajax.aspnetcdn.com/ajax/jquery.validate/1.8.1/jquery.validate.min.js类型=文/ JavaScript的>< / SCRIPT>
}<节ID =内容类=身体>
  < hgroup>
    < UL ID =帖子列表级=饲料>
      <立GT;
        <物品类=项>
          <报头GT;
            < H2类=条目标题>
                < A HREF =#相对=书签称号=永久链接这个帖子标题>
                    帐号登录
                &所述; / A>
            < / H>
            &所述p为H.;
                >>
                < / BR>
                请输入您的用户名和密码。
                < BR />
                < BR />
                @ Html.ActionLink(注册,注册),如果你没有一个帐户。
            &所述; / P>
          < /头>
        < /条>
      < /李>
      <立GT;
        <物品类=项>
          <页脚类=信息后>
            <地址类=名片作者>
                < IMG SRC =../ IMG / Hive.pngALT =单位ID =的gravatarWIDTH =175HEIGHT =175级=imageBorderRadius>< / IMG>
            < /地址>
          < /页脚>
          &所述;! - /.post-info - >
          < D​​IV CLASS =条目内容>
                    @ Html.ValidationSummary(真)
                    @using(Html.BeginForm()){
                        < D​​IV>
                            <&字段集GT;
                                <传奇>帐户信息与LT; /传说>                                < D​​IV CLASS =编辑标记>
                                    @ Html.LabelFor(M = GT; m.UserName)
                                < / DIV>
                                < D​​IV CLASS =主编场>
                                    @ Html.TextBoxFor(M = GT; m.UserName)
                                    @ Html.ValidationMessageFor(M = GT; m.UserName)
                                < / DIV>                                < D​​IV CLASS =编辑标记>
                                    @ Html.LabelFor(M = GT; m.Password)
                                < / DIV>
                                < D​​IV CLASS =主编场>
                                    @ Html.PasswordFor(M = GT; m.Password)
                                    @ Html.ValidationMessageFor(M = GT; m.Password)
                                < / DIV>                                < D​​IV CLASS =编辑标记>
                                    @ Html.CheckBoxFor(M = GT; m.RememberMe)
                                    @ Html.LabelFor(M = GT; m.RememberMe)
                                < / DIV>                                &所述p为H.;
                                    <输入类型=提交值=登录/>
                                &所述; / P>
                            < /字段集>
                        < / DIV>
         }
        < / DIV>
          &所述;! - /.entry-content - >
        < /条>
      < /李>
    < / UL>
  < / hgroup>
  <! - /#帖子列表 - >
< /节>@ Html.Partial(_的OpenID)

任何指导将是非常美联社preciated。

谢谢!


编辑:2012-02-12

正如我意见接受的答案指出,我意识到我真正想要去哪个,尽管该框架的名称,可能会在某些情况下完全可行的想法是MVVM的做法。

我正在读通过Anoop一个博客贴子,让我想起了这个问题,我想我要补充一个有趣的选择,可能是你要对与ASP.NET MVC 3框架的MVVM方法的情况下尤其有用:

注意到他如何使用在这篇文章下面的模板:的 KsigDo任务板 - 实时UI视图模型跨用户同步使用ASP.NET,SignalR,淘汰赛MVVM和EF

 <脚本类型=text / html的ID =taskTemplate>
    <李风格=列表样式图像:网址('/图像/ task.png')>
        <输入类型=复选框数据绑定=选中:完成/>
        <输入类=UI刀尖所有的数据绑定=值:标题,使:完成()!/>
        <输入类=UI键式=按钮的href =#数据绑定=点击:删除VALUE =X>< /输入>
    < /李>
< / SCRIPT>


解决方案

您将使用第一个作为主视图

  @model MyTemplate.Models.LogOnModel
@ {
     布局=〜/浏览次数母版页/布局;
     Page.Title =登录;
 }

检查此链接以获取更多信息。

的http:// weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx

编辑:


您可以拥有硕士意见hiearchy您的文章观点也可以有一个布局。

什么你也可以做的是从母版视图模型继承你的模型

〜/查看/共享/ Master.cshtml

  @model MyTemplate.Models.MasterModel
@ {
     Page.Title =登录;
 }

〜/查看/共享/ Post.cshtml

  @model MyTemplate.Models.PostModel
@ {
     布局=〜/查看/共享/ master.cshtml
 }

〜/查看/发表/ default.cshtml

  @model MyTemplate.Models.LogOnModel
@ {
     布局=〜/查看/共享/ Post.cshtml
     Page.Title =登录;
 }

在哪里LogOnModel从PostModel继承和PostModel从MasterModel继承

I'm wondering if there's an easy way to do the following:

Say I have a blog where I want a master view of the past X number of entries.

For each of those entries I have a small template for the title, the name of the author, an avatar and the body of the post.

Simple enough that I could template that for blog entries, but what if I wanted to use this template for several purposes by injecting another partial view into where I am placing the body of the post? Is there a simple way to do this in MVC3. Please forgive me if this is simple, I might be missing a search term :)

Here is an example:

Template for each "Entry":

@{
    ViewBag.Title = "_Entry";
}

<link rel="stylesheet" href="@Url.Content("~/css/post.css")" /> 

<section id="content" class="body">
  <hgroup>
    <ol id="posts-list" class="feed">
      <li>
        <article class="entry">
          <header>
            <h2 class="entry-title">
                <a href="#" rel="bookmark" title="Permalink to this POST TITLE">
                </a>
            </h2>
          </header>
          <footer class="post-info">
            <abbr class="published" title="2005-10-10T14:07:00-07:00">
                <!-- YYYYMMDDThh:mm:ss+ZZZZ -->
            </abbr>
            <address class="vcard author">
                <a class="url fn" href="#">
                </a>
            </address>
          </footer>
        </article>
      </li>
      <li>
        <article class="entry">
          <footer class="post-info">
            <address class="vcard author">
                <img src=""></img>      
            </address>
          </footer>
          <!-- /.post-info -->
          <div class="entry-content">
            <p>
            </p>
        </div>
          <!-- /.entry-content --> 
        </article>
      </li>
    </ol>
  </hgroup>
  <!-- /#posts-list --> 
</section>

and an example how it might be used in a different context (except this represents the desired end expression...I'm wondering if there's a way to take the Entry, as a view, and dynamically inject a partial view into it (in the below example, that "injection" would be for the log on view):

@model MyTemplate.Models.LogOnModel

@{
    Page.Title = "Log On";
}

@section HeadContent
{
    <link rel="stylesheet" href="@Url.Content("~/css/openid.css")" />   
    <link rel="stylesheet" href="@Url.Content("~/css/post.css")" />
}
@section ScriptSection
{
    <script type="text/javascript" src="@Url.Content("~/js/libs/openid-jquery.js")"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            openid.init('openid_identifier');
        });
    </script>
    <script src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8.1/jquery.validate.min.js" type="text/javascript"></script>
}

<section id="content" class="body">
  <hgroup>
    <ul id="posts-list" class="feed">
      <li>
        <article class="entry">
          <header>
            <h2 class="entry-title">
                <a href="#" rel="bookmark" title="Permalink to this POST TITLE">
                    Account Login
                </a>
            </h2>
            <p>
                >>
                </br>
                Please enter your username and password. 
                <br />
                <br />
                @Html.ActionLink("Register", "Register") if you don't have an account.
            </p>
          </header>
        </article>
      </li>
      <li>
        <article class="entry">
          <footer class="post-info">
            <address class="vcard author">
                <img src="../img/Hive.png" alt="units" id="gravatar" width="175" height="175" class="imageBorderRadius"></img>      
            </address>
          </footer>
          <!-- /.post-info -->
          <div class="entry-content">
                    @Html.ValidationSummary(true)
                    @using (Html.BeginForm()) {
                        <div>
                            <fieldset>
                                <legend>Account Information</legend>

                                <div class="editor-label">
                                    @Html.LabelFor(m => m.UserName)
                                </div>
                                <div class="editor-field">
                                    @Html.TextBoxFor(m => m.UserName)
                                    @Html.ValidationMessageFor(m => m.UserName)
                                </div>

                                <div class="editor-label">
                                    @Html.LabelFor(m => m.Password)
                                </div>
                                <div class="editor-field">
                                    @Html.PasswordFor(m => m.Password)
                                    @Html.ValidationMessageFor(m => m.Password)
                                </div>

                                <div class="editor-label">
                                    @Html.CheckBoxFor(m => m.RememberMe)
                                    @Html.LabelFor(m => m.RememberMe)
                                </div>

                                <p>
                                    <input type="submit" value="Log On" />
                                </p>
                            </fieldset>
                        </div>
         }
        </div>
          <!-- /.entry-content --> 
        </article>
      </li>
    </ul>
  </hgroup>
  <!-- /#posts-list --> 
</section>

@Html.Partial("_OpenId")

Any guidance would be much appreciated.

Thanks!


Edited: 2012-02-12

As noted in my comments to the accepted answer, I realized I was really trying to go for an MVVM approach which, despite the name of the framework, may be a completely viable idea in some cases.

I was reading a blog post by Anoop that reminded me of this question and I thought I would add an interesting alternative that may be especially useful in the event you are going for an MVVM approach with the ASP.NET MVC 3 framework:

notice how he uses the below template in this post: KsigDo Task Pad – Real-Time UI View Model syncing across users with ASP.NET, SignalR, Knockout MVVM and EF

<script type="text/html" id="taskTemplate">
    <li  style="list-style-image: url('/images/task.png')">
        <input type="checkbox" data-bind="checked: completed" />
        <input class="ui-corner-all" data-bind="value: title, enable: !completed()" />
        <input class="ui-button" type="button" href="#" data-bind="click: remove" value="x"></input>
    </li>
</script>

解决方案

You would use the first as a master view

@model MyTemplate.Models.LogOnModel
@ {
     Layout = "~/Views/LAYOUT OF THE MASTER PAGE" ;  
     Page.Title = "Log On";
 }

check this link for more information

http://weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx

EDIT:


You can have a hiearchy of master views your post view can also have a layout.

What you can also do is inherit your models from the master views model

"~/Views/Shared/Master.cshtml"

@model MyTemplate.Models.MasterModel
@ {
     Page.Title = "Log On";
 }

"~/Views/Shared/Post.cshtml"

@model MyTemplate.Models.PostModel
@ {
     Layout = "~/Views/Shared/master.cshtml" ;  
 }

"~/Views/Post/default.cshtml"

@model MyTemplate.Models.LogOnModel
@ {
     Layout = "~/Views/Shared/Post.cshtml" ;  
     Page.Title = "Log On";
 }

Where LogOnModel inherits from PostModel and PostModel inherits from MasterModel

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

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