在 asp.net Core 3.1 中调用 Razor 页面时传递对象
Passing An Object When A Razor Page Called in asp.net Core 3.1
所以,我是 asp.net 核心 3.1
的新手
我制作了一个页面来显示必须获取名为 postId
的 int
参数的文章,我想使用我的存储库获取具有 url 中给定 ID 的页面(postId)
所以我有 3 个问题:
1- 如何定义到我的 Razor 页面的路由以获取 postId
值?
2- 我在 My DomainClasses 层中有文章模型(它包括 title
、 content
、 pageVisits
等...)如果我想向我的页面发送一个新实例我必须做什么?
3- 我想向我的页面发送一个名为 PageToShow
的模型(一个新的文章实例,里面有信息)
我必须在 OnGet
方法中初始化此对象还是必须创建一个新的 Class?
我看到了 Link 但它对我没有帮助:(
1- How To Define A Route To My Razor Page To Get The postId value?
您可以通过设置asp-page
和参数asp-route-postId
将postId
传递给页面A。
在A页面,可以直接在OnGet
方法中接收postid参数
2- I Have The Article Model in My DomainClasses Layer ( it Includes the title , content , pageVisits and etc... ) If I Want To Send My Page A New Instance Of This What I Must Do?
首先在A Page中定义a BindProperty Article field
,传入新实体class,即可触发post方法,然后文章字段会收到你的新实体class 通过使用 asp-for
绑定每个字段。
3- I Want To Send My Page A Model Called PageToShow ( a new Instance Of Article With Info's Inside) I Must Initialize This Object In OnGet Method Or I Must Create A New Class?
这个和第二个问题一样
下面我用两个页面做一个简单的demo。
第一页ArticleDatas
用于显示所有Article模型数据,其数据的每一行都有一个link显示详细信息并将postId
传递给另一个页面A
.
A页面是显示当前postId对应的数据,然后修改数据提交按钮保存最新的数据,然后return到ArticleDatas页面
ArticleDatas.cshtml.cs:
public class ArticleDatasModel : PageModel
{
private readonly MyDbContext _context;
[BindProperty]
public List<Article> Articles { get; set; }
public ArticleDatasModel(MyDbContext context)
{
_context = context;
}
public void OnGet()
{
Articles = _context.Article.ToList();
}
}
ArticleDatas.cshtml:
@page
@model WebApplication_core_razorpage.Pages.ArticleDatasModel
@{
ViewData["Title"] = "ArticleDatas";
Layout = "~/Pages/Shared/_Layout.cshtml";
}
<h1>ArticleDatas</h1>
<table class="table table-bordered">
<tr>
<th>Id</th>
<th>title</th>
<th> </th>
</tr>
@foreach (var item in Model.Articles)
{
<tr>
<td>@item.Id</td>
<td>@item.title</td>
<td><a asp-page="./A" asp-route-postId="@item.Id">Show Details</a></td>
</tr>
}
</table>
A.cshtml.cs:
public class AModel : PageModel
{
private readonly MyDbContext _context;
[BindProperty]
public Article article { get; set; }
public AModel(MyDbContext context)
{
_context = context;
}
public void OnGet(int postId)
{
article = _context.Article.Find(postId);
}
public IActionResult OnPost()
{
Article articleNew = _context.Article.Find(article.Id);
articleNew.title = article.title;
articleNew.content = article.content;
articleNew.pageVisits = article.pageVisits;
_context.SaveChanges();
return RedirectToPage("ArticleDatas");
}
}
A.cshtml:
@page
@model WebApplication_core_razorpage.Pages.AModel
@{
ViewData["Title"] = "A";
Layout = "~/Pages/Shared/_Layout.cshtml";
}
<h1>A</h1>
<form asp-action="post">
<table class="table table-bordered">
<tr>
<th>Id</th>
<th>title</th>
<th>content</th>
<th>pageVisits</th>
</tr>
<tr>
<td><input type="text" asp-for="@Model.article.Id" hidden />@Model.article.Id</td>
<td><input type="text" asp-for="@Model.article.title" /></td>
<td><input type="text" asp-for="@Model.article.content" /></td>
<td><input type="text" asp-for="@Model.article.pageVisits" /></td>
</tr>
</table>
<input id="Submit1" type="submit" value="submit" />
</form>
测试结果如下:
所以,我是 asp.net 核心 3.1
的新手我制作了一个页面来显示必须获取名为 postId
的 int
参数的文章,我想使用我的存储库获取具有 url 中给定 ID 的页面(postId)
所以我有 3 个问题:
1- 如何定义到我的 Razor 页面的路由以获取 postId
值?
2- 我在 My DomainClasses 层中有文章模型(它包括 title
、 content
、 pageVisits
等...)如果我想向我的页面发送一个新实例我必须做什么?
3- 我想向我的页面发送一个名为 PageToShow
的模型(一个新的文章实例,里面有信息)
我必须在 OnGet
方法中初始化此对象还是必须创建一个新的 Class?
我看到了
1- How To Define A Route To My Razor Page To Get The postId value?
您可以通过设置asp-page
和参数asp-route-postId
将postId
传递给页面A。
在A页面,可以直接在OnGet
方法中接收postid参数
2- I Have The Article Model in My DomainClasses Layer ( it Includes the title , content , pageVisits and etc... ) If I Want To Send My Page A New Instance Of This What I Must Do?
首先在A Page中定义a BindProperty Article field
,传入新实体class,即可触发post方法,然后文章字段会收到你的新实体class 通过使用 asp-for
绑定每个字段。
3- I Want To Send My Page A Model Called PageToShow ( a new Instance Of Article With Info's Inside) I Must Initialize This Object In OnGet Method Or I Must Create A New Class?
这个和第二个问题一样
下面我用两个页面做一个简单的demo。
第一页ArticleDatas
用于显示所有Article模型数据,其数据的每一行都有一个link显示详细信息并将postId
传递给另一个页面A
.
A页面是显示当前postId对应的数据,然后修改数据提交按钮保存最新的数据,然后return到ArticleDatas页面
ArticleDatas.cshtml.cs:
public class ArticleDatasModel : PageModel
{
private readonly MyDbContext _context;
[BindProperty]
public List<Article> Articles { get; set; }
public ArticleDatasModel(MyDbContext context)
{
_context = context;
}
public void OnGet()
{
Articles = _context.Article.ToList();
}
}
ArticleDatas.cshtml:
@page
@model WebApplication_core_razorpage.Pages.ArticleDatasModel
@{
ViewData["Title"] = "ArticleDatas";
Layout = "~/Pages/Shared/_Layout.cshtml";
}
<h1>ArticleDatas</h1>
<table class="table table-bordered">
<tr>
<th>Id</th>
<th>title</th>
<th> </th>
</tr>
@foreach (var item in Model.Articles)
{
<tr>
<td>@item.Id</td>
<td>@item.title</td>
<td><a asp-page="./A" asp-route-postId="@item.Id">Show Details</a></td>
</tr>
}
</table>
A.cshtml.cs:
public class AModel : PageModel
{
private readonly MyDbContext _context;
[BindProperty]
public Article article { get; set; }
public AModel(MyDbContext context)
{
_context = context;
}
public void OnGet(int postId)
{
article = _context.Article.Find(postId);
}
public IActionResult OnPost()
{
Article articleNew = _context.Article.Find(article.Id);
articleNew.title = article.title;
articleNew.content = article.content;
articleNew.pageVisits = article.pageVisits;
_context.SaveChanges();
return RedirectToPage("ArticleDatas");
}
}
A.cshtml:
@page
@model WebApplication_core_razorpage.Pages.AModel
@{
ViewData["Title"] = "A";
Layout = "~/Pages/Shared/_Layout.cshtml";
}
<h1>A</h1>
<form asp-action="post">
<table class="table table-bordered">
<tr>
<th>Id</th>
<th>title</th>
<th>content</th>
<th>pageVisits</th>
</tr>
<tr>
<td><input type="text" asp-for="@Model.article.Id" hidden />@Model.article.Id</td>
<td><input type="text" asp-for="@Model.article.title" /></td>
<td><input type="text" asp-for="@Model.article.content" /></td>
<td><input type="text" asp-for="@Model.article.pageVisits" /></td>
</tr>
</table>
<input id="Submit1" type="submit" value="submit" />
</form>
测试结果如下: