数据注释 - asp.net mvc 4

时间:2021-03-26 16:39:22

I m new to MVC. I have make a model LoginModel

我是MVC的新手。我已经制作了一个模型LoginModel

{
    [Required (ErrorMessage="Username is required field.")]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required (ErrorMessage="Password is required field.")]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }
}

and design a Form for it with id = "CustomLogin" and its action="User/Validate" and Validate action returns JsonResult

并为其设计一个表单,其中id =“CustomLogin”,其action =“User / Validate”,Validate操作返回JsonResult

In web.config,

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

In my _Layout.cshtml,

在我的_Layout.cshtml中,

@Scripts.Render("~/bundles/jqueryval")

Now, if i directly click on "Submit" button of form (CustomLogin), so it prevents me to jump User/Validate that is right..

现在,如果我直接点击表单(CustomLogin)的“提交”按钮,那么它阻止我跳转正确的User / Validate ..

But in external Javascript say mycode.js, I have also written

但在外部Javascript中说mycode.js,我也写过

 $(document).ready(
     function()
     {
          $('#CustomLogin').submit(
            function(e)
            {
               alert ('i executed.');
               //suppose, here i wrote JSON call, it will also be executed
            });
     });

It is also executed. So is it OK..??

它也被执行。好吧.. ??

My thinking is if I click on submit button, without passing any value,

我的想法是,如果我点击提交按钮,没有传递任何价值,

Unobstrusive javascript prevents jumping control over my post request "User/Validate",

不引人注目的JavaScript可以防止对我的帖子请求“用户/验证”进行跳转控制,

then it should also prevent my submit event. To solve this, I have used $("form").valid() in my submit event handler.

那么它也应该阻止我的提交事件。为了解决这个问题,我在提交事件处理程序中使用了$(“form”)。valid()。

like this,

  if ( $("#CustomLogin").valid() ) 
  {
       alert('I executed.');
  }

I think Unobstrusive javascript makes form state : invalid or valid...

我认为Unobstrusive javascript使表单状态:无效或有效...

**> BUT, my question is if I need to use $("form").valid(), then WHAT IS

**>但是,我的问题是我是否需要使用$(“form”)。有效(),然后是什么

USE OF Unobstrusive javascript for validation...???? **

使用不引人注目的javascript进行验证...... ???? **

So is this OK.. or i m doing in wrong manner

这样可以......或者我做错了

1 个解决方案

#1


0  

Because your model is invalid, doesn't necessarily mean you don't want to communicate with the server. It just means you don't want to submit that model.

因为您的模型无效,并不一定意味着您不想与服务器通信。它只是意味着您不想提交该模型。

#1


0  

Because your model is invalid, doesn't necessarily mean you don't want to communicate with the server. It just means you don't want to submit that model.

因为您的模型无效,并不一定意味着您不想与服务器通信。它只是意味着您不想提交该模型。