【问题标题】:Passing An Object When A Razor Page Called in asp.net Core 3.1在 asp.net Core 3.1 中调用 Razor 页面时传递对象
【发布时间】:2020-05-13 08:08:34
【问题描述】:

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

我创建了一个页面来显示一篇文章,该文章必须获取一个名为 postIdint 参数,并且我想使用我的存储库来获取具有 url (postId) 中给出的 id 的页面

所以我有 3 个问题:

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

2- 我的 DomainClasses 层中有文章模型(它包括 titlecontentpageVisits 等...)如果我想向我的页面发送一个新实例我必须这样做会吗?

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

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

【问题讨论】:

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


    【解决方案1】:

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

    您可以通过设置asp-page 和参数asp-route-postIdpostId 传递到页面A。

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

    2- 我的 DomainClasses 层中有文章模型(它包括标题、内容、pageVisits 等...)如果我想向我的页面发送一个新实例我必须做什么?

    首先在A Page中定义a BindProperty Article field,传入新的实体类,可以触发post方法,然后Article字段通过asp-for绑定各个字段来接收你的新实体类。

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

    这与第二个问题相同。


    下面我用两个页面做一个简单的demo。

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

    页面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>
    

    这是测试结果:

    【讨论】:

    • 谢谢你,这真的很有用,解决了我的问题:)
    猜你喜欢
    • 2020-05-06
    • 2020-07-19
    • 1970-01-01
    • 2021-01-27
    • 2021-10-30
    • 1970-01-01
    • 2021-03-01
    • 2021-12-25
    • 2019-11-12
    相关资源
    最近更新 更多