简体   繁体   English

剃刀页面-Post上的RouteData为null

[英]Razor pages - RouteData null on Post

I am trying to learn MVC and Razor pages (I am new to web development) and I am having an NullReferenceException on Post because RouteData is null. 我正在尝试学习MVC和Razor页面(我是Web开发的新手),并且因为RouteData为null,所以Post上出现了NullReferenceException。 I don't have this issue on the Get. 我在Get上没有这个问题。 The page loads normally, but as soon as I click the Filter button, the exceptions shows. 该页面正常加载,但是一旦我单击“过滤器”按钮,就会显示异常。 I can't even debug the OnPostAsync because it doesn't even enter the method. 我什至无法调试OnPostAsync,因为它甚至都没有输入方法。 This is my page: 这是我的页面:

<div class="row">
    <div class="col-md-4">
        <form method="post">
            <div class="form-group">
                <label asp-for="Input.UserName" class="control-label"></label>
                <input asp-for="Input.UserName" class="form-control" />
            </div>
            <div class="form-group">
                <label asp-for="Input.Email" class="control-label"></label>
                <input asp-for="Input.Email" class="form-control" />
            </div>
            <div class="form-group">
                <input type="submit" value="Filter" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>

<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.UserList[0].UserName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.UserList[0].Email)
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.UserList)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.UserName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Email)
                </td>
            </tr>
        }
    </tbody>
</table>

And this is the code: 这是代码:

public class UsersModel : PageModel
{
    [BindProperty]
    public _userModel _UserModel { get; set; }

    [BindProperty]
    public InputModel Input { get; set; }

    internal IList<_userModel> userList = new List<_userModel>();
    public IList<_userModel> UserList { get => userList; set => userList = value; }

    public class InputModel
    {
        [Display(Name = "Username")]
        public string UserName { get; set; }

        [Display(Name = "Email")]
        public string Email { get; set; }
    }

    public async Task<IActionResult> OnGetAsync()
    {
        //Add all users to UserList
        return Page();
    }
    public async Task<IActionResult> OnPostAsync()
    {
        //Filter UserList
        return Page();
    }
}

Found the issue. 找到了问题。 I was using [BindProperty] on a model not being used on the page: 我在页面上未使用的模型上使用[BindProperty]

[BindProperty]
public _userModel _UserModel { get; set; }

I just removed the [BindProperty] and it was resolved. 我刚刚删除了[BindProperty] ,它已解决。

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

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