locked
Razor Pages and passing parms? RRS feed

  • Question

  • User1120430333 posted

    I want to convert the HTML that was working as a MVC view over to  a Razor page. I need to pass the ID to the OnPost(), but I have not seen any example of how to pass the item.AuthorID directly not on an a-tag similar to what in happening on an ActionLink. 

    I guess I could use a controller and a view  in the Razor pages project. But do I have to do this to achieve what I was doing with a MVC view with a controller? 

    @page
    @model IndexModel
    @{
        ViewData["Title"] = "Author Page";
    }
    
    <h1>Authors</h1>
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Authors</title>
    </head>
    <body>
        <form method="post">
    
            <a asp-page="/Author/Create">Create</a>
    
            <br /><br />
            <table border="1" cellpadding="10">
                <tr>
                    <th>AuthorID</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th colspan="4">Actions</th>
                </tr>
                @foreach (var item in Model.AuthorVM.Authors)
                {
                    <tr>
                        <td>@item.AuthorID</td>
                        <td>@item.FirstName</td>
                        <td>@item.LastName</td>
                        <td>
                            @Html.ActionLink("Edit", "Edit", new { id = item.AuthorID })
                        </td>
                        <td>
                            @Html.ActionLink("Detail", "Detail", new { id = item.AuthorID })
                        </td>
    
                        <td>
                            @Html.ActionLink("Delete", "Delete", new { id = item.AuthorID },
                                new { onclick = "return confirm('Are you sure you wish to delete this author?');" })
                        </td>
    
                    </tr>
                }
            </table>
        </form>
    </body>
    </html>
    
    

    Thursday, February 20, 2020 9:53 PM

Answers

  • User475983607 posted

    Yes , I want to do a Get as I call OnGet() on the  EditModel page and I want to pass a ID parm to OnGet(). Cannot an a-tag be used for this? I want to click edit for a given Author in the list for Authors and edit the Author based on the AuthorID and its data pulled from the database. I don't want to use a controller, and I want  the EditModel to do it.

    Yes, you can use a tag helper.  

    <div>
        <a asp-page="edit" asp-route-id="2">Edit</a>
    </div>
    @page
    @model RazorDemo.EditModel
    @{
        ViewData["Title"] = "Edit";
    }
    
    <h1>Edit</h1>
    
    <div>
        You passed @Model.ID
    </div>
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    
    namespace RazorDemo
    {
        public class EditModel : PageModel
        {
            public void OnGet(int id)
            {
                ID = id;
            }
    
            public int ID { get; set; }
        }
    }

    I recommend the following site for learning Razor Pages.

    https://www.learnrazorpages.com/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 21, 2020 2:36 AM
  • User-854763662 posted

    Hi DA924 ,

    For Razor page, you may try “@Url.Page”.

    Razor page did not follow Controller/Action route; it is routed based on “Pages/Folder/PageName”.

    For "./Users/Detail", it will route Pages Folder-> Users Folder-> Detail.cshtml. You could try code below to generate the same result.

    <a href="@Url.Page("./Users/Detail",new { id= item.AuthorID})">Detail</a>

    Best Regards,

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 21, 2020 6:31 AM
  • User1120430333 posted

    Thanks for the help I got it to work.

    @page
    @model IndexModel
    @{
        ViewData["Title"] = "Author Page";
    }
    
    <h1>Authors</h1>
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Authors</title>
    </head>
    <body>
            <a asp-page="/Author/Create">Create</a> <a asp-page="/Author/Index">Cancel</a>
    
            <br /><br />
            <table border="1" cellpadding="10">
                <tr>
                    <th>AuthorID</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th colspan="4">Actions</th>
                </tr>
                @foreach (var item in Model.AuthorVM.Authors)
                {
                    <tr>
                        <td>@item.AuthorID</td>
                        <td>@item.FirstName</td>
                        <td>@item.LastName</td>
                        <td>
                            <a href="/Author/Edit?id=@item.AuthorID">Edit</a>
                        </td>
                        <td>
                            <a href="/Author/Detail?id=@item.AuthorID">Detail</a>
                        </td>
    
                        <td>
                            <a href="/Author/Delete?id=@item.AuthorID" 
                               onclick="return confirm('Are you sure, you want to delete this author?')">Delete</a>
                        </td>
                    </tr>
                }
            </table>
    </body>
    </html>
    
    
    
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using WebRazor3.x.Models;
    
    namespace WebRazor3.x.Pages.Author
    {
        public class EditModel : PageModel
        {
            private readonly IAuthorDM _authorDm;
    
            public EditModel(IAuthorDM authorDm)
            {
                _authorDm = authorDm;
            }
    
            [BindProperty]
            public AuthorVM.Author Author { get; set; } = new AuthorVM.Author();
    
            public IActionResult OnGet(int id)
            {
                Author = _authorDm.Find(id);
                return Page();
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 21, 2020 7:06 AM

All replies

  • User475983607 posted

    DA924

    I want to convert the HTML that was working as a MVC view over to  a Razor page. I need to pass the ID to the OnPost(), but I have not seen any example of how to pass the item.AuthorID directly not on an a-tag similar to what in happening on an ActionLink. 

    I guess I could use a controller and a view  in the Razor pages project. But do I have to do this to achieve what I was doing with a MVC view with a controller? 

    @page
    @model IndexModel
    @{
        ViewData["Title"] = "Author Page";
    }
    
    <h1>Authors</h1>
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Authors</title>
    </head>
    <body>
        <form method="post">
    
            <a asp-page="/Author/Create">Create</a>
    
            <br /><br />
            <table border="1" cellpadding="10">
                <tr>
                    <th>AuthorID</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th colspan="4">Actions</th>
                </tr>
                @foreach (var item in Model.AuthorVM.Authors)
                {
                    <tr>
                        <td>@item.AuthorID</td>
                        <td>@item.FirstName</td>
                        <td>@item.LastName</td>
                        <td>
                            @Html.ActionLink("Edit", "Edit", new { id = item.AuthorID })
                        </td>
                        <td>
                            @Html.ActionLink("Detail", "Detail", new { id = item.AuthorID })
                        </td>
    
                        <td>
                            @Html.ActionLink("Delete", "Delete", new { id = item.AuthorID },
                                new { onclick = "return confirm('Are you sure you wish to delete this author?');" })
                        </td>
    
                    </tr>
                }
            </table>
        </form>
    </body>
    </html>
    

    There are no submit button in the markup.  You do have links which submits a HTTP GET not a POST.

    If you want to POST then add the HTML Form to the loop and set the route parameter.

    <form asp-action="myaction" asp-controller="mycontroller" asp-route-id="@item.Id">
    
    </form>

    https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/anchor-tag-helper?view=aspnetcore-3.1#asp-route-value

    Thursday, February 20, 2020 10:00 PM
  • User1120430333 posted

    Yes , I want to do a Get as I call OnGet() on the  EditModel page and I want to pass a ID parm to OnGet(). Cannot an a-tag be used for this? I want to click edit for a given Author in the list for Authors and edit the Author based on the AuthorID and its data pulled from the database. I don't want to use a controller, and I want  the EditModel to do it.

    Thursday, February 20, 2020 10:41 PM
  • User475983607 posted

    Yes , I want to do a Get as I call OnGet() on the  EditModel page and I want to pass a ID parm to OnGet(). Cannot an a-tag be used for this? I want to click edit for a given Author in the list for Authors and edit the Author based on the AuthorID and its data pulled from the database. I don't want to use a controller, and I want  the EditModel to do it.

    Yes, you can use a tag helper.  

    <div>
        <a asp-page="edit" asp-route-id="2">Edit</a>
    </div>
    @page
    @model RazorDemo.EditModel
    @{
        ViewData["Title"] = "Edit";
    }
    
    <h1>Edit</h1>
    
    <div>
        You passed @Model.ID
    </div>
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    
    namespace RazorDemo
    {
        public class EditModel : PageModel
        {
            public void OnGet(int id)
            {
                ID = id;
            }
    
            public int ID { get; set; }
        }
    }

    I recommend the following site for learning Razor Pages.

    https://www.learnrazorpages.com/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 21, 2020 2:36 AM
  • User-854763662 posted

    Hi DA924 ,

    For Razor page, you may try “@Url.Page”.

    Razor page did not follow Controller/Action route; it is routed based on “Pages/Folder/PageName”.

    For "./Users/Detail", it will route Pages Folder-> Users Folder-> Detail.cshtml. You could try code below to generate the same result.

    <a href="@Url.Page("./Users/Detail",new { id= item.AuthorID})">Detail</a>

    Best Regards,

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 21, 2020 6:31 AM
  • User1120430333 posted

    Thanks for the help I got it to work.

    @page
    @model IndexModel
    @{
        ViewData["Title"] = "Author Page";
    }
    
    <h1>Authors</h1>
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Authors</title>
    </head>
    <body>
            <a asp-page="/Author/Create">Create</a> <a asp-page="/Author/Index">Cancel</a>
    
            <br /><br />
            <table border="1" cellpadding="10">
                <tr>
                    <th>AuthorID</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th colspan="4">Actions</th>
                </tr>
                @foreach (var item in Model.AuthorVM.Authors)
                {
                    <tr>
                        <td>@item.AuthorID</td>
                        <td>@item.FirstName</td>
                        <td>@item.LastName</td>
                        <td>
                            <a href="/Author/Edit?id=@item.AuthorID">Edit</a>
                        </td>
                        <td>
                            <a href="/Author/Detail?id=@item.AuthorID">Detail</a>
                        </td>
    
                        <td>
                            <a href="/Author/Delete?id=@item.AuthorID" 
                               onclick="return confirm('Are you sure, you want to delete this author?')">Delete</a>
                        </td>
                    </tr>
                }
            </table>
    </body>
    </html>
    
    
    
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using WebRazor3.x.Models;
    
    namespace WebRazor3.x.Pages.Author
    {
        public class EditModel : PageModel
        {
            private readonly IAuthorDM _authorDm;
    
            public EditModel(IAuthorDM authorDm)
            {
                _authorDm = authorDm;
            }
    
            [BindProperty]
            public AuthorVM.Author Author { get; set; } = new AuthorVM.Author();
    
            public IActionResult OnGet(int id)
            {
                Author = _authorDm.Find(id);
                return Page();
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 21, 2020 7:06 AM