C# ASP.NET Core 5 Razor页面:如何正确使用局部视图并验证它';什么是模型状态?

C# ASP.NET Core 5 Razor页面:如何正确使用局部视图并验证它';什么是模型状态?,c#,asp.net,asp.net-core,razor-pages,asp.net-core-razor-pages,C#,Asp.net,Asp.net Core,Razor Pages,Asp.net Core Razor Pages,我正在使用ASP.NETCore5Razor页面 我的目标是拥有一组可以在多个页面上使用的局部视图(出于可重用性目的)。每个部分视图都有一个表单,该表单具有自己的自定义post事件处理程序(它将由包含此部分视图的页面的代码隐藏处理) 注意:某些页面可能包含两个或更多不同的局部视图!我需要部分视图模型相互独立地进行验证(在两个独立的自定义事件处理程序中) 下面是我今天使用的简化代码局部视图模型(包含用户的一些数据): \u UserPartial.cshtml(显示该用户数据): 问题是userM

我正在使用ASP.NETCore5Razor页面

我的目标是拥有一组可以在多个页面上使用的局部视图(出于可重用性目的)。每个部分视图都有一个表单,该表单具有自己的自定义post事件处理程序(它将由包含此部分视图的页面的代码隐藏处理)

注意:某些页面可能包含两个或更多不同的局部视图!我需要部分视图模型相互独立地进行验证(在两个独立的自定义事件处理程序中)

下面是我今天使用的简化代码局部视图模型(包含用户的一些数据):

\u UserPartial.cshtml(显示该用户数据):

问题是userModel.ModelState始终有效,即使Name姓氏为空: 看起来UserModel根本无效

我有一种强烈的感觉,我用的是错误的方法(不是他们应该用的方法)


那么我的代码怎么了?如何正确使用局部视图并验证其模型状态?非常感谢您的帮助。

部分视图不需要页面模型。只需将其添加为Razor视图

Index.cshtml.cs

[BindProperty]
public User userModel { get; set; }

[BindProperty]
public Message messageModel { get; set; }

[TempData]
public string StatusMessage { get; set; }

public void OnGet()
{
    userModel = new User();
}

public IActionResult OnPostUserEdited()
{
    ModelState.Clear();
    if (!TryValidateModel(userModel))
    {
        return Page();
    }

    StatusMessage = "User data was saved!";
    return RedirectToPage();
}

public IActionResult OnPostMessageEdited()
{
    ModelState.Clear();
    if (!TryValidateModel(messageModel))
    {
        return Page();
    }

    StatusMessage = "Message data was saved!";
    return RedirectToPage();
}
Index.cshtml:

<div class="text-center" id="userPartialView">
    @{await Html.RenderPartialAsync("_UserPartial", Model.userModel);}
</div>

<div class="text-center" id="messagePartialView">
    @{await Html.RenderPartialAsync("_MessagePartial", Model.messageModel);}
</div>

@{wait Html.RenderPartialAsync(“_UserPartial”,Model.userModel);}
@{wait Html.RenderPartialAsync(“_MessagePartial”,Model.messageModel);}

有些页面可能包含两个甚至更多不同的局部视图-我已更新了我的问题。因此,除了
public User userModel{get;set;}
之外,它还可以是例如我的主页上的
public messageModel{get;set;}
。我需要两个模型相互独立地进行验证(在两个独立的定制事件处理程序中)。我举个例子,您提供的ModelState.IsValid只有在部分视图模型(userModel和messageModel)都有效时才为true。是否有一种方法可以确定例如只有userModel字段是正确的?好的,我更新了我的答案。他们确实验证了所有的模型,但是您可以在处理程序中做一些事情,首先清除modelstate,然后验证指定的模型。谢谢。这正是我所需要的。
@page
@model IndexModel
@{
    ViewData["Title"] = "Main page";
}    

@if (!String.IsNullOrWhiteSpace(@Model.StatusMessage))
{
    <div class="text-center">
        <h4 class="text-warning">@Model.StatusMessage</h4>
    </div>
}    

<div class="text-center" id="mainView">
    <p>Some text in a main view</p>
    <p>Some <a href="https://docs.microsoft.com/aspnet/core">link</a> in a main view.</p>
</div>

<div class="text-center" id="userPartialView">
    @{await Html.RenderPartialAsync("_UserPartial", IndexModel.userModel);}
</div>
//Some other Partial View (which contains some data for a message)
<div class="text-center" id="userPartialView">
    @{await Html.RenderPartialAsync("_MessagePartial", IndexModel.messageModel);}
</div>
public class IndexModel : PageModel
{
    public static UserModel userModel { get; set; }
    //A model for some other Partial View (which contains some data for a message)
    public static MessageModel messageModel { get; set; }
    [TempData]
    public string StatusMessage { get; set; }        

    public IActionResult OnGet()
    {
        userModel = new UserModel();
        messageModel = new MessageModel();
        return Page();
    }

    public IActionResult OnPostUserEdited()
    {
        if (!userModel.ModelState.IsValid)
        {
            return Page();
        }

        StatusMessage = "User data was saved!";
        return RedirectToPage();
    }
}
[BindProperty]
public User userModel { get; set; }

[BindProperty]
public Message messageModel { get; set; }

[TempData]
public string StatusMessage { get; set; }

public void OnGet()
{
    userModel = new User();
}

public IActionResult OnPostUserEdited()
{
    ModelState.Clear();
    if (!TryValidateModel(userModel))
    {
        return Page();
    }

    StatusMessage = "User data was saved!";
    return RedirectToPage();
}

public IActionResult OnPostMessageEdited()
{
    ModelState.Clear();
    if (!TryValidateModel(messageModel))
    {
        return Page();
    }

    StatusMessage = "Message data was saved!";
    return RedirectToPage();
}
<div class="text-center" id="userPartialView">
    @{await Html.RenderPartialAsync("_UserPartial", Model.userModel);}
</div>

<div class="text-center" id="messagePartialView">
    @{await Html.RenderPartialAsync("_MessagePartial", Model.messageModel);}
</div>