.NET Core第25天_PartialTagHelper的使用_主檢視傳遞資料(單一property跟整個物件)

 
在之前

有介紹PartialView的功用跟寫法
主要用於某個主視圖中的部分內容,常用在部分內容更新。
於Controller當中會使用 PartialView()語法來回傳
可返回指定的View或Model Entity,跟View()使用一樣。
跟一般的View不同之處在於沒有引用佈局頁。


PartialView使用語法寫法
就有分透過HtmlHelper的方式
兩個同步的寫法方式
@Html.Partial("分布視圖路徑")
@{ Html.RenderPartial("分布視圖路徑");}
兩個非同步的寫法方式
@await Html.PartialAsync("分布視圖路徑")
@{await Html.RenderPartialAsync("分布視圖路徑")}

另外還有
PartialView的tag寫法
<partial name="部分檢視完整路徑(若是在當前目錄下則只需要檔名不需要副檔名)" />



新增PartialController.cs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Net5App6.Controllers
{
    public class PartialController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }
}


跟對應Index檢視

1
2
3
4
5
6
7
8
9
@{ 
    ViewData["Title"] = "PartialTagHelper";
}

<h4>主檢視</h4>

<div>
    <partial name="APartial" />
</div>


另外還有APartial檢視

1
2
3
<div style="color:blue">
    部分檢視
</div>


運行效果




若今天是在不同於當前View的部分檢視則要寫完整路徑包含副檔名
比方今天新增BPartial.cshtml在./Views/Home目錄下




ViewModel傳遞至Partial Tag
準備一個ViewModel
PartialViewModel.cs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Net5App6.Models
{
    public class PartialViewModel
    {
        public string Title { get; set; }
        public int Sum { get; set; }
    }
}

PartialController.cs微調整後

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
using Microsoft.AspNetCore.Mvc;
using Net5App6.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Net5App6.Controllers
{
    public class PartialController : Controller
    {
        public IActionResult Index()
        {
            PartialViewModel model = new PartialViewModel();
            model.Title = "Pizza";
            model.Sum = 600;
            return View(model);
        }
    }
}


1.傳送物件中的單一個property
可以透過for=物件屬性



./Views/Partial/APartial.cshtml

1
2
3
4
5
6
<div style="color:blue">
    A部分檢視
</div>
<p>從主檢視傳來的Title:
    @Model
</p>


./Views/Partial/Index.cshtml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
@{ 
    ViewData["Title"] = "PartialTagHelper";
}

<h4>主檢視</h4>

@model PartialViewModel
<div>
    <partial name="APartial" for="Title" />
</div>

<div>
    <partial name="/Views/Home/BPartial.cshtml" />
</div>


運行效果
可以看到主檢視有成功把Model屬性資料傳遞給APartial




2.傳送整個物件
可以在屬性去擴充一個物件本體

PartialViewModel.cs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Net5App6.Models
{
    public class PartialViewModel
    {
        public string Title { get; set; }
        public int Sum { get; set; }
        public PartialViewModel _PartialViewModel { get; set; }
    }
}


PartialController.cs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
using Microsoft.AspNetCore.Mvc;
using Net5App6.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Net5App6.Controllers
{
    public class PartialController : Controller
    {
        public IActionResult Index()
        {
            PartialViewModel model = new PartialViewModel();
            model.Title = "Pizza";
            model.Sum = 600;
            model._PartialViewModel = model;
            return View(model);
        }
    }
}


./Views/Partial/Index.cshtml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
@{ 
    ViewData["Title"] = "PartialTagHelper";
}

<h4>主檢視</h4>

@model PartialViewModel
<div>
    <partial name="APartial" for="_PartialViewModel" />
</div>

<div>
    <partial name="/Views/Home/BPartial.cshtml" />
</div>

這裡我們for 指定物件屬性存自己本身


APartial.cshtml

1
2
3
4
5
6
7
8
9
@model PartialViewModel
<div style="color:blue">
    A部分檢視
</div>
<p>
    從主檢視傳來的物件資料:
    <span>@Model.Title</span>
    <span>@Model.Sum</span>
</p>

其中第一行可以省略,只是幫助在使用@Model存取屬性時候可以有提示訊息。

運行效果
















留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題