ASP.net MVC3 DropDownListFor INT要求DataAnnotation [英] ASP.net MVC3 DropDownListFor int Required DataAnnotation

查看:111
本文介绍了ASP.net MVC3 DropDownListFor INT要求DataAnnotation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有触发客户端验证由DropDownListFor创建的选择列表的方法吗?这得到了助手创建的选择列表似乎并没有得到数据-VAL或数据-VAL-要求的文本输入获取客户端验证属性。

Is there a way to trigger client-side validation for the select list created by DropDownListFor? The select list that gets created by the helper doesn't seem to get the "data-val" or "data-val-required" attributes that text inputs get for client-side validation.

验证确实发生了,当我检查ModelState.IsValid服务器端,然后,在随后的页面加载显示的验证消息。

The validation does occur on the server-side when I check ModelState.IsValid, and the validation message is then displayed on the subsequent page load.

我设置的列表中的默认选项请选择...,因为我希望用户必须选择一个选项(而不是让它在列表中选择的第一项)。

I set a default option of "Please Select..." on the list because I want the user to have to choose an option (as opposed to having it select the first item in the list).

我的视图模型:

public partial class ProvinceVM
{
    [Required]
    [Range(1, int.MaxValue)]
    public int CountryId { get; set; }
    [Required]
    [StringLength(16)]
    public string Abbreviation { get; set; }
    [Required]
    [StringLength(64)]
    public string Name { get; set; }
}

在code。在查看:

The code in the View:

<div class="editor-label">
    @Html.LabelFor(model => model.CountryId, "Country")
</div>
<div class="editor-field">
    @Html.DropDownListFor(model => model.CountryId, (IEnumerable<SelectListItem>)ViewBag.CountryId, "Please Select...") 
    @Html.ValidationMessageFor(model => model.CountryId)
</div>

和我的控制器code:

And my controller code:

[HttpPost]
public ActionResult Create(ProvinceEditVM provinceVM)
{
    if (ModelState.IsValid)
    {
        var province = new Province() { CountryId = provinceVM.CountryId.Value, Abbreviation = provinceVM.Abbreviation, Name = provinceVM.Name };
        _repo.Add<Province>(province);
        _repo.SaveChanges();
        return RedirectToAction("index");  
    }
    ViewBag.CountryId = new SelectList(_repo.GetQuery<Country>().OrderBy(x => x.Name), "CountryId", "Name", provinceVM.CountryId);
    return View(provinceVM);
}

我的东西已经尝试过:

Things I've already tried:


  • 使视图模型的CountryId属性可空INT(INT?)

  • 删除范围属性为CountryId财产

  • 重命名ViewBag.CountryId别的东西(如果存在与视图模型的属性名有些抵触)

我可以很容易地使用jQuery所需要的价值,但我要确保我不忽视了已经建成的东西;特别是因为进一步下跌的路上,我想补充本地化的文化错误消息。

I can easily make the value required using jQuery, but I want to make sure I'm not overlooking something that is already built in; especially since further down the road I'd like to add localized culture error messages.

推荐答案

居然能得到它的工作做的我在帖子的末尾提到的所有三件事情的组合。取得该物业的视图模型为空的,必须的,必须的ViewBage.CountryId重命名为别的东西(我把它改名为CountryList)。

Was actually able to get it working by doing a combination of all three things I mentioned at the end of my post. Made the property on the View Model nullable, required, and had to rename the ViewBage.CountryId to something else (I renamed it to CountryList).

我想通了通过添加的SelectList属性我ProvinceVM把它弄出来的ViewBag的话一下子把数据-VAL *属性已显示在列表上起来。

I figured it out by adding a SelectList property to my ProvinceVM to get it out of the ViewBag, then all of a sudden the data-val* attributes were showing up on the list.

这篇关于ASP.net MVC3 DropDownListFor INT要求DataAnnotation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆