【问题标题】:Select List Focus on invalidate in MVC 3 with Jquery Validation使用 Jquery Validation 在 MVC 3 中选择 List Focus on invalidate
【发布时间】:2011-08-01 19:36:51
【问题描述】:

在我的无效表单上的 MVC3 应用程序中,焦点未设置在第一个选择列表上(如果选择列表数据为空/无效)。选择列表无效但未设置焦点。如果是输入类型,即文本框、收音机等,它可以正常工作。我想知道缺少什么??

Cool...here comes the model and view...
**Model**

 public class RegisterModel
    {
        [Remote("CheckDuplicateUserName","Account")]
        [Display(Name = "User name")]
        [Required(ErrorMessage = "This field is required.")]
        [StringLength(30)]
        public string UserName { get; set; }


        [DataType(DataType.EmailAddress)]
        [Display(Name = "Email address")]
        [Remote("CheckDuplicateEmail", "Account")]
        [Required(ErrorMessage = "This field is required.")]
        public string Email { get; set; }


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

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        [StringLength(30)]
        public string ConfirmPassword { get; set; }


        //additional fields

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

        [Display(Name = "Address")]
        [StringLength(100)]
        public string Address { get; set; }

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


    }

**View** 


@model MyShowCase.Models.RegisterModel
@{
    ViewBag.Title = "Register";
}

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm("Register","Account"))
{
    @Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.")
    <div id="register">
       <div class="editor-label">
                @Html.LabelFor(m => m.UserName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.UserName) *
                @Html.ValidationMessageFor(m => m.UserName)
                  <span class="instruction"> username should be alphanumeric without any special characters.</span>

            </div>
            <div class="editor-label">
                @Html.LabelFor(m => m.Password)
            </div>
            <div class="editor-field">
                @Html.PasswordFor(m => m.Password) *   @Html.ValidationMessageFor(m => m.Password)
                <span class="instruction"> Passwords are required to be a minimum of @ViewBag.PasswordLength characters in length.</span>


            </div>
            <div class="editor-label">
                @Html.LabelFor(m => m.ConfirmPassword)
            </div>
            <div class="editor-field">
                @Html.PasswordFor(m => m.ConfirmPassword) *
                @Html.ValidationMessageFor(m => m.ConfirmPassword)
            </div>
            <div class="editor-label">
                @Html.LabelFor(m => m.Name)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.Name) *
                @Html.ValidationMessageFor(m => m.Name)
                  <span class="instruction"> Specify your Full Name here.</span>

            </div>

     <div class="editor-label">
                @Html.LabelFor(m => m.City)
            </div>
            <div class="editor-field">
                <select id="City" name="City">
                    <option value="" selected="selected"></option>
                    <option >Dubai</option>
                    <option >Abu Dhabi</option>

                </select>
                *
                @Html.ValidationMessageFor(m => m.City)
            </div>

    </div>
}

Here is all what i have, the problem is that focus is not set on "City" Dropdown if City is not selected. Its validated but the focus in not set....


web.config 
  <appSettings>
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />

    </appSettings>

【问题讨论】:

  • 你们能给我们看一些代码吗?如果没有它,很难判断您的应用可能会崩溃。
  • 真的吗?这就是你所有的代码?你的 jQuery 和/或 C#/VB 在哪里?
  • 我已经写了“MVC3”,在 MVC3 中仅供参考,您必须在标记中指定 class="required" 或使用必需属性注释您的实体。其余由 MVC 3 完成...
  • 所以,再次请给我们看一些代码。点击您帖子上的编辑按钮,粘贴您的一些代码,然后帮助我们帮助您。向我们展示您的模型、更多标记和控制器代码。

标签: jquery forms asp.net-mvc-3 validation


【解决方案1】:

缺少所需的类。将选择列表的样式设置为 class='required' 并且它工作正常。

【讨论】:

    【解决方案2】:

    如果您想将下拉列表集中在按钮单击验证上,那么您只需在 CSS 文件中添加一个类。

    .form-control.input-validation-error {
    border: 1px solid #b94a48;
    }
    

    它在我的代码中工作。试试看。

    【讨论】:

      猜你喜欢
      • 2016-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多