简体   繁体   English

在Razor页面使用路由参数显示ID值

[英]Using Route Parameters in Razor Pages to Display value of ID

I'm currently using an input textbox and a button to display the value entered from a database using a partial view as shown in the images below我目前正在使用一个输入文本框和一个按钮来显示使用局部视图从数据库输入的值,如下图所示

在此处输入图像描述

在此处输入图像描述

Index.cshtml索引.cshtml

@page
@model IndexModel
@{ ViewData["Title"] = "Index"; }

<form method="get">
    <table>
        <tr>
            <td>
                @Html.TextBox("TxtDepartment")
            </td>
            <td>
                <button type="button" id="DepartmentSearch">Search</button>
            </td>
        </tr>
    </table>
</form>
<br />

<table>
    <tr>
        <td><div id="DepartmentResult"></div></td>&nbsp;
        <td><div id="EmployeeResult"></div></td>
    </tr>
</table>

<form method="get">
    <label>Department Name:</label>
    <input type="text" id="DeptName" />
    <label>Photo File Name:</label>
    <input type="text" id="NameResult" />
</form>

@section Scripts {
    <script>
        $("#DepartmentSearch").click(function()
        {
            $.ajax(
            {
                url: "/Index?handler=DisplayDepartment",
                type: "GET",
                data: { value: $("#TxtDepartment").val() },
                headers: { RequestVerificationToken: $('input:hidden[name="__RequestVerificationToken"]').val() },
                success: function(data) { $("#DepartmentResult").html(data); }
            });
        });
    </script>
}

Index.cshtml.cs索引.cshtml.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using PracticeApp.Models;
using System.Linq;

namespace PracticeApp.Pages
{
    public class IndexModel : PageModel
    {
        public CompanyContext _context;

        public IndexModel(CompanyContext context) { _context = context; }

        public PartialViewResult OnGetDisplayDepartment(int value)
        {
            return Partial("_DisplayDepartmentPartial", _context.Departments.Where(x => x.DepartmentId == value).ToList());
        }

What I'm trying to do is instead of using a textbox and button, I want the Property ID value in the URL as when the page loads have the partial view load based on the value in the URL我想要做的不是使用文本框和按钮,而是希望 URL 中的属性 ID 值,因为当页面加载时,部分视图加载基于 URL 中的值

This is my Department model这是我的部门 model

using System.ComponentModel.DataAnnotations;

namespace PracticeApp.Models
{
    public partial class Department
    {
        [Display(Name = "ID")] public int DepartmentId { get; set; }
        [Display(Name = "Name")] public string DepartmentName { get; set; } = null!;
    }
}

I've tried @page {id?} but I' stuck on the AJAX portion我试过 @page {id?} 但我卡在 AJAX 部分

在此处输入图像描述

在此处输入图像描述

Try this way:试试这样:

Index.cshtml.cs:索引.cshtml.cs:

Create an Id property to receive the parameters in the url:创建一个Id属性来接收url中的参数:

[BindProperty(SupportsGet = true)]
public int Id { get; set; }

public PartialViewResult OnGetDisplayDepartment(int value)
{
    return Partial("_DisplayDepartmentPartial", _context.Departments.Where(x => x.Id == value).ToList());
}

Index.cshtml:索引.cshtml:

@page "{id?}"
...
@section Scripts {
    <script>
        $(document).ready(function(){
            $.ajax(
                {
                    url: "/Index?handler=DisplayDepartment",
                    type: "Get",
                    data: { value: @Model.Id },
                    headers: { RequestVerificationToken: $('input:hidden[name="__RequestVerificationToken"]').val() },
                    success: function (data) { $("#DepartmentResult").html(data); }
                });
        })
        $("#DepartmentSearch").click(function () {
            $.ajax(
                {
                    url: "/Index?handler=DisplayDepartment",
                    type: "Get",
                    data: { value: $("#TxtDepartment").val() },
                    headers: { RequestVerificationToken: $('input:hidden[name="__RequestVerificationToken"]').val() },
                    success: function (data) { $("#DepartmentResult").html(data); }
                });
        });
    </script>
}

Test Result:测试结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM