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

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

OtherModel.cs:

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

namespace MvcPartialViewTest.Models
{
    public class OtherModel
    {
        public string Name
        {
            get
            {
                return "李四";
            }

        }
        public string Remark
        {
            get
            {
                return "航小学生.";
            }
        }
        public double Score
        {
            get
            {
                return 100;
            }
        }
    }
}

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");
        }
        public ActionResult GetPartialView2()
        {
            return PartialView("PartialDataFromOtherModel", new OtherModel());
        }
    }
}

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>

4)PartialDataFromOtherModel.cshtml:

@model MvcPartialViewTest.Models.OtherModel

<fieldset>
    <legend>OtherModel</legend>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Name)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Name)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Remark)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Remark)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Score)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Score)
    </div>
</fieldset>

D.创建View文件:

Index.cshtml:

@using MvcPartialViewTest.Models
@model  LoginModel

@{
    ViewBag.Title = "Index";
}
<h2>1.没有参数传递的PartialView</h2>
<h2>@Html.Partial("PartialLink")</h2>
<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>
<hr />
<h2>6.使用Html.Action,通过Action获取OtherMode数据的PartialView</h2>
<h2>@Html.Action("GetPartialView2")</h2>
E.效果图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gCodeTop 格码拓普 老师

您的鼓励.我的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值