MVC调用部分视图PartialView

2016/12/29 15:37
阅读数 1K
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Demo2017.Models
{
    /* ~/Models/LoginModel.cs  */
    public class LoginModel
    {
        public string Name
        {
            get
            {
                return "张星";
            }
        }
        public string Remark
        {
            get
            {
                return "计算机系1班";
            }
        }
        public double Score
        {
            get
            {
                return 99.12;
            }
        }
    }

    public class OtherModel
    {
        public string Name
        {
            get
            {
                return "李琳琳";
            }
        }
        public string Remark
        {
            get
            {
                return "旅游系1班";
            }
        }
        public double Score
        {
            get
            {
                return 100;
            }
        }
    }  
}

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

namespace Demo2017.Controllers
{
    /* ~/Controllers/HomeController.cs  */
    public class HomeController : Controller
    {
        // GET: /Home/  
        public ActionResult Index()
        {
            ViewData.Model = new LoginModel();
            return View();
        }
        public ActionResult GetPartialView3()
        {
            return PartialView("View3", new OtherModel());
        }
    }  
}
@using Demo2017.Models
@model  LoginModel

@{
    // ~/Views/Home/Index.cshtml
    ViewBag.Title = "Index";
}
@Scripts.Render("~/bundles/jquery")  
<script type="text/javascript">
    $(function () {
        //Jquery调用PartialView
        $("#loadDiv").load("/Home/GetPartialView3");
    })
</script>
<hr />
<h2>1.直接从LoginModel中获取数据的PartialView</h2>
@Html.Partial("View1")
<hr />
<h2>2.从View中间接获取LoginModel数据的PartialView</h2>
@Html.Partial("View2", Model.Score)
<hr />
<h2>3.使用Html.Action,通过Action获取OtherMode数据的PartialView</h2>
<h2>@Html.Action("GetPartialView3")</h2>  

<div id="loadDiv"></div>


@using Demo2017.Models
@* ~/Views/Shared/View1.cshtml *@
@model LoginModel
<h2>@Model.Name</h2>
<h2>@Model.Remark</h2>
<h2>@Model.Score</h2>

@model System.Double
@* ~/Views/Shared/View2.cshtml *@
<h2>@Model</h2>

@model Demo2017.Models.OtherModel
@* ~/Views/Shared/View3.cshtml *@
<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>

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部