DropDownList验证

时间:2016-09-08 02:00:19

标签: c# asp.net asp.net-mvc

如果我在没有从DropDownList中选择值的情况下点击提交按钮,我的创建操作中的DropDownList上会收到验证错误消息。

我得到的验证错误是值""无效。我希望验证错误消息说明状态是必需的!

我将所需的验证属性添加到ViewModel上的StId属性,但是没有解决问题。

客户模式

    public class Customer
{
    public int CustId { get; set; }
    public string CustFirstName { get; set; }
    public string CustLastName { get; set; }

    public int StId { get; set; }
    public State State { get; set; }
}

州模式

    public class State
{
    public int StId { get; set; }
    public string StAbbr { get; set; }

    public List<Customer> Customers { get; set; }
}

CustomerFormViewModel

    public class CustomerFormViewModel
{
    public int CustId { get; set; }

    [Required(ErrorMessage = "First Name is required!")]
    [Display(Name = "First Name")]
    public string CustFirstName { get; set; }

    [Required(ErrorMessage = "Last Name is required!")]
    [Display(Name = "Last Name")]
    public string CustLastName { get; set; }

    [Required(ErrorMessage = "State is required!")]
    [Display(Name = "State")]
    public int StId { get; set; }

    public IEnumerable<State> States { get; set; }
}

CustomerController

    public class CustomerController : Controller
{
    private OneClickAwayDbContext _context;

    public CustomerController(OneClickAwayDbContext context)
    {
        _context = context;
    }

    public ActionResult Index()
    {
        return View(_context.Customers.ToList());
    }

    public ActionResult Create()
    {
        var states = _context.States.ToList();
        var viewModel = new CustomerFormViewModel
        {
            States = states
        };

        return View(viewModel);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(CustomerFormViewModel vm)
    {
        if (ModelState.IsValid)
        {
            var customer = new Customer();
            {
                customer.CustFirstName = vm.CustFirstName;
                customer.CustLastName = vm.CustLastName;
                customer.StId = vm.StId;
            }

            _context.Customers.Add(customer);
            _context.SaveChanges();
            return RedirectToAction("Index");
        }

        else
        {
            var stateViewModel = new CustomerFormViewModel
            {
                States = _context.States.ToList()
            };
            return View("Create", stateViewModel);
        }
    }
}

Create.chtml

    @using (Html.BeginForm("Create", "Customer"))
{
    <div class="form-group">
        @Html.LabelFor(c => c.CustFirstName)
        @Html.TextBoxFor(c => c.CustFirstName, new { @class = "form-control" })
        @Html.ValidationMessageFor(c => c.CustFirstName)
    </div>

    <div class="form-group">
        @Html.LabelFor(c => c.CustLastName)
        @Html.TextBoxFor(c => c.CustLastName, new { @class = "form-control" })
        @Html.ValidationMessageFor(c => c.CustLastName)
    </div>

    <div class="form-group">
        @Html.LabelFor(s => s.StId)
        @Html.DropDownListFor(s => s.StId, new SelectList(Model.States, "StId", "StAbbr"), "", new { @class="form-control"})
        @Html.ValidationMessageFor(s => s.StId)
    </div>

    <div class="form-group">
        <button type="submit" class="btn btn-primary">Submit</button>
    </div>
}

2 个答案:

答案 0 :(得分:0)

在web.config中添加此行

false

在布局页面中包含以下jquery文件。

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

如果需要任何验证,使用此提交按钮点击事件不会进入您的[HttpPost]操作。

答案 1 :(得分:0)

请在cshtml文件中包含jquery.validate.js和jquery.validate.unobtrusive.js

<script src="~/lib/jquery-validation/dist/jquery.validate.js"></scri‌​pt>     
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.uno‌​btrusive.js"></scrip‌​t>