0

我在 asp .net mvc3 c# 中创建一个应用程序。我创建了一个下拉列表,但无法使用 Jquery 进行客户端验证。

我查看了许多与之相关的文章,并知道这是 asp .net mvc3 中的一个错误。但是,没有任何解决方法适用于我的情况。

我的代码如下:

实体

[Required(ErrorMessage = "Furnishing is required.")]
[Display(Name = "Furnishing*")]
public int FurnishedType { get; set; }

看法:

<script src="@Url.Content("~/Scripts/jquery-1.8.2.js")" type="text/javascript"></script>
<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>

<div class="editor-label">
    @Html.LabelFor(model => model.Property.FurnishedType)
    @Html.DropDownListFor(model => model.Property.FurnishedType, Model.FurnishedTypes, "--Select One--")
</div>
<div class="editor-field add-property-error">            
    @Html.ValidationMessageFor(model => model.Property.FurnishedType)
</div>

控制器:

public ActionResult AddProperty()
{
    AddPropertyViewModel viewModel = new AddPropertyViewModel
    {
        ...
        FurnishedTypes = websiteRepository.GetFurnishedTypeSelectList(-1),
        ...
    };
    return View(viewModel);
}

public IEnumerable<SelectListItem> GetFurnishedTypeSelectList(int selectedId)
{
    return db.FurnishedType
        .OrderBy(x => x.FurnishedTypeDescription)
        .ToList()
        .Select(x => new SelectListItem
        {
            Value = x.FurnishedTypeId.ToString(),
            Text = x.FurnishedTypeDescription
        });
}

有一个数据库,其值如下

1 Furnished
2 Part Furnished
...

用于填充选择列表。

现在,这是一个必填字段,但我无法进行客户端验证以查看用户是否选择了任何值?任何帮助将不胜感激

4

2 回答 2

0

如果您将 -1 分配给“--select one--”

有些东西应该是这样的:

$(document).ready(function () {
    $("input[type='submit']").click(function (e) {
        if($("select").val()==="-1"){
           e.preventDefault();
           // display error msg beside ddl too
      }
    });
}
)
于 2012-11-07T23:55:13.183 回答
0

我的建议是执行以下操作

在视图中

@using (Html.BeginForm("AddProperty", "Home", FormMethod.Post, new { id = "myForm" }))
{ 
    <div class="editor-label">
        @Html.LabelFor(model => model.Property.FurnishedType)
        @Html.DropDownListFor(model => model.Property.FurnishedType, Model.FurnishedTypes)
    </div>
    <div class="editor-field add-property-error">            
        @Html.ValidationMessageFor(model => model.Property.FurnishedType)
    </div>
}

<script type="text/javascript">

    $.validator.addMethod('selectNone', function (value, element) {
        return this.optional(element) || value != -1;
    }, "Furnishing is required.");


     $("#myForm").validate({
         rules: { FurnishedType: { selectNone: true }}
     });

</script>

在控制器中

public IEnumerable<SelectListItem> GetFurnishedTypeSelectList(int selectedId)
{
    List<SelectListItem> Lists = new List<SelectListItem>();
    Lists = db.FurnishedType
        .OrderBy(x => x.FurnishedTypeDescription)
        .ToList()
        .Select(x => new SelectListItem
        {
            Value = x.FurnishedTypeId.ToString(),
            Text = x.FurnishedTypeDescription
        });

    Lists.Insert(0, new SelectListItem { Text = "--Select One--", Value = "-1" });

    return Lists;
}
于 2012-11-08T04:04:15.650 回答