簡體   English   中英

剃刀頁面-Post上的RouteData為null

[英]Razor pages - RouteData null on Post

我正在嘗試學習MVC和Razor頁面(我是Web開發的新手),並且因為RouteData為null,所以Post上出現了NullReferenceException。 我在Get上沒有這個問題。 該頁面正常加載,但是一旦我單擊“過濾器”按鈕,就會顯示異常。 我什至無法調試OnPostAsync,因為它甚至都沒有輸入方法。 這是我的頁面:

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

這是代碼:

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();
    }
}

找到了問題。 我在頁面上未使用的模型上使用[BindProperty]

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

我剛剛刪除了[BindProperty] ,它已解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM