Asp.net Mvc4中的验证问题 [英] Validation issue in Asp.net Mvc4

查看:88
本文介绍了Asp.net Mvc4中的验证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的模特。

  public   class  Model1 
{
[必需(ErrorMessage = 用户名不得为空白 )]
public string 用户名{获得; set ; }
[必需(ErrorMessage = 密码应匹配)]
< span class =code-keyword> public
string 密码{ get ; set ; }
}





这是我的控制器。

  public  class HomeController:Controller 
{
[HttpGet]
public ActionResult 索引()
{
return 视图();
}

[HttpPost]
public ViewResult 索引(Model1 model1)
{
if (ModelState.IsValid)
{
if (model1.Username == adm&& model1 .Password == adm
{
return 查看 FWMenu,model1);
}
其他
{
返回 视图();
}
}
返回 查看();
}





这是我的看法。



< pre lang =sql>< h1>
登录< / h1>
< br />
@ Html .ValidationSummary()
@ using @ Html .BeginForm())
{
< p> @ Html.TextBoxFor(x => x.Username)< / p>
< p> @ Html.TextBoxFor(x => x.Password)< / p>
< br />
< p>
< input type = submit value = 登录 />< / p>

}
< / div>





我的验证工作正常。我的问题是,当我将我的用户名和密码字段留空并单击登录时,整个页面都会刷新以显示验证。但我不希望我的整个页面得到刷新。可能吗?如果是,那么解决方案是什么?

解决方案

1。一种解决方案是使用部分视图或面板作为登录控件(用户名,密码及其验证)和AJAX调用,仅重新呈现用于用户登录控件的部分视图/面板。

<你可以在下一篇文章中找到类似解决方案的例子:

http://www.aspdotnet-pools.com/2014/09/ajax-login-form-using-jquery-in-aspnet.html [ ^ ]


您可以在网页上使用jquery.validate.js和jquery.validate.unobtrusive.js来显示模型验证的客户端错误。

This is my model.

public class Model1
    {
        [Required(ErrorMessage="Username must not be blank")]
        public string Username { get; set; }
        [Required(ErrorMessage="Password should match")]
        public string Password { get; set; }
    }



This is my controller.

public class HomeController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ViewResult Index(Model1 model1)
        {
            if (ModelState.IsValid)
            {
                if (model1.Username == "adm" && model1.Password == "adm")
                {
                    return View("FWMenu", model1);
                }
                else
                {
                    return View();
                }
            }
            return View();
        }



This is my view.

<h1>
		Log-in</h1>
	<br />
	@Html.ValidationSummary()
	@using (@Html.BeginForm())
 {
		<p>@Html.TextBoxFor(x => x.Username)</p>
		<p>@Html.TextBoxFor(x => x.Password)</p>
		<br />
		<p>
			<input type="submit" value="Login" /></p>
		
 }
</div>



My validation is working perfectly. My problem is that when i am keeping my username and password field blank and clicking on login, the whole page is getting refreshed to show me the validation. But i do not want my full page to get refreshed. Is it possible? If yes then what is the solution?

解决方案

1. One solution is to use partial view or a panel for your login controls (username, password and their validations) and AJAX call for re-rendering only the partial view/panel used for user login controls.

2.You can find example for a similar solution in the next article:
http://www.aspdotnet-pools.com/2014/09/ajax-login-form-using-jquery-in-aspnet.html[^]


you can use jquery.validate.js and jquery.validate.unobtrusive.js on your webpage to display client side error's for model validations.


这篇关于Asp.net Mvc4中的验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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