Query String Parameters In Razor Pages 2020

In this article, we will understand Query String Parameters. How to Use Query String Parameters and How to Pass Query String Parameters In Asp.Net Razor Pages?

Query String Parameters

If we want to pass data from the Display template to Code behind file which is Page Model in our case then we can use the asp-data-parameter_name tag helper to pass data from the display template to the Page Model class. This parameter catches in OnGet or OnPost method.

Now we have to achieve functionality like when we click on View button we can get in detail information about Employee in table format. For this, we need to create one Details Razor page in our Employee folder which resides under Page folder.

First, we need to pass data from View for that use below the line of code.

<tr><td><a  class="btn btn-primary" asp-page="/Employee/Details" asp-route-ID="@emp.ID">View</a></td></tr>

Now we need to cacth this Id in OnGet method for that we pass parameter in OnGet() method.

public void OnGet(string id)
        {

        }

In this Model binding maps id parameter value to OnGet() method id parameter. And by default id is pass as query string parameter in URL.

We don’t have any method which can get Employee Information by using Id so we need to create this in Interface and then we need to implement this in our DBRepository class as we implement this interface in this class.

The code for Interface and DbRepository have shown below.

public interface IEmployeeRepos
    {
        IEnumerable<Employee> GetAllEmployees();
        Employee GetEmployee(string Id);
    }

    public class DBRepository: IEmployeeRepos
    {
        private List<Employee> _empList;
        public DBRepository()
        {
            _empList = new List<Employee>() {
                new Employee(){ Dept=Department.HR,Email="sagarjaybhay@gmail.com",ID="A101",Name="Sagar Jaybhay",Photopath=@"sagar jaybhay.png"},
                new Employee(){Dept=Department.IT,Email="rani@hotmail.com",ID="A201",Name="Rani",Photopath=@"rani.jpg" },
                new Employee(){ Dept=Department.Testing,Email="raja@gmail.com",ID="A301",Name="Raja",Photopath=@"ram.png"},
                new Employee(){ Dept=Department.Testing,Email="raghu@gmail.com",ID="A401",Name="Ragu",Photopath=""}

            };
            }


        public IEnumerable<Employee> GetAllEmployees()
        {
            return this._empList;
        }

        public Employee GetEmployee(string Id)
        {
            return this._empList.Where(s => s.ID == Id).SingleOrDefault();
        }
    }

Here is one note: When you use the anchor tag element with an asp-page tag helper then not use default href attribute if you do so you will get an error. Following is the error.

An unhandled exception occurred while processing the request.

InvalidOperationException: Cannot override the ‘href’ attribute for <a>. An <a> with a specified ‘href’ must not have attributes starting with ‘asp-route-‘ or an ‘asp-action’, ‘asp-controller’, ‘asp-area’, ‘asp-route’, ‘asp-protocol’, ‘asp-host’, ‘asp-fragment’, ‘asp-page’ or ‘asp-page-handler’ attribute.

GitHub:- https://github.com/Sagar-Jaybhay/AspNerRazorPages

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Related posts