码迷,mamicode.com
首页 > Web开发 > 详细

AspNet MVC4 教学-22:Asp.Net MVC4 Partial View 技术快速应用Demo

时间:2015-06-02 13:33:23      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

A.创建Basic类型的MVC项目.

B.Model目录下,创建文件:

LoginModel.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcPartialViewTest.Models
{
    public class LoginModel
    {
        public string Name 
        {
            get
            {
                return "张三";
            }
        
        }
        public string  Remark
        {
            get
            {
                return "航大学生.";
            }
        }
          public double Score
        {
            get
            {
                return 99.12;
            }
        }
    }
}

C.创建HomeController.cs文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcPartialViewTest.Models;

namespace MvcPartialViewTest.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
                  
            ViewData.Model = new LoginModel();
            return View();
        }
        public ActionResult GetPartialView()
        {
            return PartialView("PartialLink");
        }
    }
}

C.创建相应的PartialView:

1)PartialLink.cs:

<a href="http://www.sina.com.cn">新浪</a> <a href="http://www.sohu.com">搜狐</a> <a href="http://www.exesoft.cn">行易软件</a>
2)PartialDataFromLoginModel.cshtml:
@using MvcPartialViewTest.Models
@model LoginModel
<h2>@Model.Name</h2>
<h2>@Model.Remark</h2>

3)PartialDataFromView.cshtml:

@model System.Double
<h2>@Model</h2>

D.创建View文件:

Index.cshtml:

@using MvcPartialViewTest.Models
@model LoginModel

@{
    ViewBag.Title = "Index";
}
<h2>1.没有参数传递的PartialView</h2>
@Html.Partial("PartialLink")
<hr />
<h2>2.直接从LoginModel中获取数据的PartialView</h2>
@Html.Partial("PartialDataFromLoginModel")
<hr />
<h2>3.从View中间接获取LoginModel数据的PartialView</h2>
@Html.Partial("PartialDataFromView", Model.Score)
<hr />
<h2>4.从控制器直接载入分布视图,不再套用主版视图</h2>
<h2>@Html.ActionLink("取得PartView","GetPartialView")</h2>
<hr />
<h2>5.结合上面创建的的Action,使用Html.Action再次载入</h2>
<h2>@Html.Action("GetPartialView")</h2>
E.效果图:

技术分享


AspNet MVC4 教学-22:Asp.Net MVC4 Partial View 技术快速应用Demo

标签:

原文地址:http://blog.csdn.net/vinglemar/article/details/46324979

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!