当局部视图出现在视图中时,如何显示局部视图模型错误? [英] How to display Partial view Model errors when partial view is in the view?

查看:109
本文介绍了当局部视图出现在视图中时,如何显示局部视图模型错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Asp.net Core 2.0项目,并在index视图中显示了partial view. partial view的模型带有一些验证错误.

I am working on the Asp.net Core 2.0 project and have a partial view that is shown in the index view. This partial view have a Model with some validation errors.

您可以继续查看我的模型,局部视图,控制器和...

You can continue to see my Model,Partial view,controller and ...

LoginViewModel

public class LoginViewModel
{
    [Display(Name = "UserName")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Please Enter UserName")]
    public string UserName { get; set; }

    [Display(Name = "Password")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Please Enter Password")]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [Display(Name = "Remember Me")]
    public bool RememberMe { get; set; }
}

我在_loginpartialView中将LoginViewModel用作model.在每个input

I have used LoginViewModel as model in _loginpartialView. also there are asp-validation-for to show validation error after each input

_loginpartialView.cshtml

@model partialView.Models.LoginViewModel

    <div id="login">
        <span class="header-lastnews">Login</span>
        <div class="hr"></div>

        <form asp-controller="Account" asp-action="Login" method="post">


            <div class="form-group">
                <label asp-for="UserName" class="col-sm-4 control-label"></label>
                <div class="col-sm-8">
                    <input asp-for="UserName" class="form-control" />
                    <span asp-validation-for="UserName" class="text-danger"></span>
                </div>
            </div>


            <div class="form-group">
                <label asp-for="Password" class="col-sm-4 control-label"></label>
                <div class="col-sm-8">
                    <input asp-for="Password" class="form-control" />
                    <span asp-validation-for="Password" class="text-danger"></span>
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <div class="checkbox">
                        <label asp-for="RememberMe">
                            <input asp-for="RememberMe" />
                            @Html.DisplayNameFor(m => m.RememberMe)
                        </label>
                    </div>
                </div>
            </div>

            <div class="row" style="width: 100%">
                <div class="form-group pull-left ">
                    <button type="submit" class="btn btn-success">Enter</button>
                </div>
            </div>
        </form>
    </div>

我已经使用@Html.Partial渲染并显示了index中的partial view 请注意index视图位于HomeController

I have used @Html.Partial to render and show partial view in index Be careful that the index view is inside the HomeController

Index.cshtml

@{
     ViewData["Title"] = "Home Page";
}

@Html.Partial("_loginpartialView")

最后是我的AccountController

AccountController

    [HttpPost]
    public async Task<IActionResult> Login(LoginViewModel model)
    {
        if (ModelState.IsValid)
        {
            //Success Login
            //do Something
        }
        else
        {
            //if ModelState is invalid
            return PartialView("_loginpartialView", model);
        }

        //if UserName Or Password is incorrect
        return PartialView("_loginpartialView", model);
    }

但是问题是,当我单击submit按钮而不输入输入值时,会打开一个新页面,并且partial view和模型错误会显示在新页面上. 如何在同一视图中而不是新页面中的partial view中显示模型错误?

But the problem is that when I click on the submit button without entering the inputs value, a new page opens and partial view and Model errors show on new page. How can i show model errors in partial view in the same view not a new page?

推荐答案

这是标准的表单提交标签,该标签发送POST请求并返回新页面作为预期响应:

This is a standard form submission tag, which sends POST request and returns new page as expected response:

<form asp-controller="Account" asp-action="Login" method="post">

由于要返回带有验证错误的部分视图到同一页面,因此必须使用基于AJAX的表单.您可以将如上所述的标准表单提交转换为基于AJAX的表单,该表单在同一主视图下返回部分视图,如下例所示:

Since you want to return partial view into same page with validation errors, you must use AJAX-based form. You can convert standard form submit like above into AJAX-based form which returns partial view at the same main view, as in example below:

<form id="partialform" asp-controller="Account" asp-action="Login" data-ajax="true" data-ajax-update="#partialform" data-ajax-mode="replace">
    <!-- form inner elements here -->
</form>

您应该记住的最重要的事情:

The most important things you should remember:

  1. data-ajax="true"必须存在以确保表单接受AJAX数据.

  1. data-ajax="true" must be exist to ensure form accepts AJAX data.

设置id属性并将该ID值提供给data-ajax-update属性,以便AJAX回调后面的jQuery选择器可以识别它.

Setting id attribute and provide that ID value to data-ajax-update property so that the jQuery selector behind AJAX callback recognizes it.

data-ajax-mode="replace"设置为在AJAX success结果返回到服务器响应后,替换先前的表单元素及其内部元素.

data-ajax-mode="replace" set to replace previous form element together with its inner elements after AJAX success result to response from server.

注意:data-ajax-mode="replace"替换表单内部元素是由在后台运行的jquery.unobtrusive-ajax.js脚本完成的.

Note: The form inner element replacement with data-ajax-mode="replace" is done by jquery.unobtrusive-ajax.js script which runs in background.

这篇关于当局部视图出现在视图中时,如何显示局部视图模型错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆