在asp.net Core 3.1中调用剃刀页面时传递对象

时间:2020-05-13 08:08:34

标签: c# asp.net-core razor-pages asp.net-core-3.1

所以,我是asp.net core 3.1的新手

我制作了一个页面来显示必须抓住名为int的{​​{1}}参数的文章,并且我想使用我的存储库来获取具有url postId中给出的ID的页面< / p>

所以我有3个问题:

1-如何为我的剃须刀页面定义路由以获取(postId)值?

2-我的DomainClasses层中有文章模型(它包括postIdtitlecontent等)如果我想重新发送页面我该怎么办?

3-我想向我的页面发送一个名为pageVisits的模型(其中包含信息的新文章实例) 我必须使用PageToShow方法初始化此对象,还是必须创建一个新类?

我看到了This链接,但对我没有帮助:(

1 个答案:

答案 0 :(得分:0)

1-如何定义到我的Razor页面的路由以获取postId值?

您可以通过设置postId和参数asp-pageasp-route-postId传递到A页。

在A页中,您可以通过OnGet方法直接接收postId参数。

2-如果我想向我的页面发送一个我必须做的新实例,则我在DomainClasses层中有文章模型(包括标题,内容,pageVisits等)。

首先在A页面中定义a BindProperty Article field,传递新的实体类,您可以触发post方法,然后Article字段将通过使用asp-for绑定每个字段来接收新的实体类。 / p>

3-我要向我的页面发送一个名为PageToShow(带有信息的文章的新实例)的模型,我必须在OnGet方法中初始化该对象还是必须创建一个新类?

这与第二个问题相同。


下面,我用两个页面来制作一个简单的演示。

第一页ArticleDatas用于显示所有Article模型数据,其数据的每一行都有一个链接,该链接显示详细信息并将postId传递到另一页A。 / p>

页面A将显示与当前postId对应的数据,然后修改数据提交按钮以保存最新数据,然后返回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>

这是测试结果:

enter image description here

相关问题