简体   繁体   English

ASP.Net MVC验证(服务器端)

[英]ASP.Net MVC Validation (server side)

asp.net mvc server side validation when the javascript is disabled in the browser? 在浏览器中禁用javascript时,asp.net mvc服务器端验证? i used "remote" in my modal class it validates only when the javascript is enabled it doesnt validate when the javascript is disabled. 我在我的模态类中使用“远程”它只在启用了javascript时验证它在禁用javascript时不验证。

Scenario for my problem is i have a table in my db with a column "code" with the datatype varchar. 我的问题的场景是我的数据库中有一个表,其中包含数据类型为varchar的列“代码”。 any one inserts the data they must insert the unique code. 任何人都插入数据,他们必须插入唯一的代码。 Please do help me out 请帮帮我

I would suggest to forget about remote because if you are using code first entity framework, you can't have more that one unique column in your table. 我建议忘记remote因为如果你使用代码第一个实体框架,你的表中不能有更多的unique列。 I would just write code for it like this: 我会像这样编写代码:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
    if (ModelState.IsValid)
    {
        // Insert a new user into the database
        using (UsersContext db = new UsersContext())
        {
            UserProfile email = db.UserProfiles.FirstOrDefault(u => u.Email.ToLower() == model.Email.ToLower());
            try
            {
                // Check if email already exists
                if (email == null)
                {
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { Email = model.Email });
                    WebSecurity.Login(model.UserName, model.Password);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("Email", "Email address already exists. Please enter a different email address.");
                }
            }
            catch (MembershipCreateUserException e)
            {

                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }
    }

Replace the email with the property you want to validate. 将电子邮件替换为您要验证的属性。 At post, this will compare with entries with what already exists in your database, and depending on results, it will give you feedback. 在帖子中,这将与数据库中已存在的条目进行比较,并根据结果,它将为您提供反馈。 Throws exception if such data exists. 如果存在此类数据,则抛出异常。

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

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