Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a simple strongly-typed view.

@model GoldForGold.Models.LogonModel
@{
    ViewBag.Title = "Logins";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

Logins
@using (Html.BeginForm()) {

Account Information
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName, new { id = "txtUserName" })
@Html.ValidationMessageFor(m => m.UserName)
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password, new { id = "txtPassword" })
@Html.ValidationMessageFor(m => m.Password)
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)

<input type="submit" value="Log On" onclick="getcredentials()" />
}

Model code is here.

public class LogonModel
{
    [Required(ErrorMessage="please enter username")]
    [Display(Name = "User name")]
    public string UserName { get; set; }

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

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

I see nothing is happening even when I do not enter username and password.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
177 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here is the complete solution:

  1. Add the following scripts to your view:

    <script src='@Url.Content("~/Scripts/jquery-1.8.2.js")' type='text/javascript'></script>    
    <script src='@Url.Content("~/Scripts/jquery.validate.js")' type='text/javascript'>         </script>
    <script src='@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")' type='text/javascript'></script>
    
  2. Enable Validation in Web.Config:

    <appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    </appSettings>
    
  3. Add validation element in the view:

    @Html.ValidationMessageFor(model => model.UserName)
    
  4. Add Required attribute to you model`s element.

    [Required(ErrorMessage="please enter username")]
    public string UserName { get; set; }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...