日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

MVC 之 Partial View 用法

發布時間:2023/12/4 c/c++ 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MVC 之 Partial View 用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Partial View 顧名思義就是Html代碼片段,因此可以用Partial View 把部分的Html或顯示邏輯包裝起來,方便多次使用。

Partial View 需要放在Views/Shared 目錄下,任何Controlller 下的Action 或 View 都可以載入。

?

如何載入Partial View?

MVC 的 HTML 輔助方法有個專門的方法載入分部View,方法名稱為Partial.

  • ?? Partial有以下四種方式調用

方法原型

使用范例

Partial(HtmlHelper,String)

Html.Partial("CustomerListControl")

Partial(HtmlHelper,string,Object)

Html.Partial("CustomerListControl",Model)

Partial(HtmlHelper,string,ViewDataDictionary)

Html.Partial("CustomerListControl",ViewData["Model"])

Partial(HtmlHelper,string,Object,ViewDataDictionary)

Html.Partial("CustomerListControl",Model,ViewData["Model"])

?

?

  • 使用控制器載入分部View

? ? ? public ActionResult ?CustomerListControl()
{

? ?Return PartialView();
}

  • 使用 Html.Action 載入分部View

@Html.Action("CustomerListControl")

?

如何實現?

1 ?Models

???using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

?

namespace Step1.Models

{

? ? public class Product

? ? {

? ? ? ? public string Name

? ? ? ? {

? ? ? ? ? ? get;

? ? ? ? ? ? set;

? ? ? ? }

?

? ? ? ? public string Banner

? ? ? ? {

? ? ? ? ? ? get;

? ? ? ? ? ? set;

? ? ? ? }

? ? }

}

?

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

namespace Step1.Models

{

? ? public class OrderModel

? ? {

? ? ? ? public Customer Customer

? ? ? ? {

? ? ? ? ? ? get;

? ? ? ? ? ? set;

? ? ? ? }

? ? ? ? public List<Product> ProductList

? ? ? ? {

? ? ? ? ? ? get;

? ? ? ? ? ? set;

? ? ? ? }

? ? }

}

2 ?DAL

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using Step1.DAL;

using Step1.Models;

namespace Step1.DAL

{

? ? public class DBContext

? ? {

? ? ? ? public static OrderModel GetOrderList()

? ? ? ? {

? ? ? ? ? ? OrderModel model = new OrderModel();

? ? ? ? ? ? model.Customer = new Customer() { CustomerID = "10000", CompanyName = "redwave" };

? ? ? ? ? ? model.ProductList = new List<Product>();

? ? ? ? ? ? for (int i = 0; i < 10; i++)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? Product p = new Product();

? ? ? ? ? ? ? ? p.Banner = string.Format("Banner{0}", i.ToString());

? ? ? ? ? ? ? ? p.Name = string.Format("ProductMame{0}", i.ToString());

? ? ? ? ? ? ? ? model.ProductList.Add(p);

? ? ? ? ? ? }

? ? ? ? ? ? return model;

? ? ? ? }

? ? }

}

?

3 ?Controller

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using Step1.DAL;

namespace Step1.Controllers

{

? ? public class PartialViewController : Controller

? ? {

? ? ? ? //

? ? ? ? // GET: /PartialView/

?

? ? ? ? public ActionResult Index()

? ? ? ? {

? ? ? ? ? ? var model= ? DBContext.GetOrderList();

? ? ? ? ? ?

? ? ? ? ? ? return View(model);

? ? ? ? }

?

? ? }

}

?

4 ?Partial View

?

@using Step1.Models;

@using System.Collections;

@model IEnumerable<Product>

<table border="1" >

? ? <tr >

? ? ? ? <td>

? ? ? ? ? ? Name

? ? ? ? </td>

? ? ? ? <td>

? ? ? ? ? ? Banner

? ? ? ? </td>

? ? </tr>

? ? @foreach (var item in Model)

? ? {

? ? ? ? <tr>

? ? ? ? ? ? <td>

? ? ? ? ? ? ? ? @item.Name

? ? ? ? ? ? </td>

? ? ? ? ? ? <td>

? ? ? ? ? ? ? ? @item.Banner

? ? ? ? ? ? </td>

? ? ? ? </tr>

? ? }

? ?

</table>

?

5 ?View

using?Step1.Models;
@model?OrderModel
@{
????ViewBag.Title?=?"Index";
}

<h2>Index</h2>
<div>
????<div>
????????@Model.Customer.CompanyName
????</div>
????<div>
????????@Model.Customer.CustomerID
????</div>
????<div>
????????@Html.Partial("CustomerListControl",@Model.ProductList)
????</div>
</div>

?

6 項目結構

?

?

7 運行結果

?


轉載于:https://blog.51cto.com/12953214/1942283

總結

以上是生活随笔為你收集整理的MVC 之 Partial View 用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。