MVC 5 DropDownList客户端验证

时间:2014-05-11 04:42:32

标签: c# asp.net-mvc-4 razor asp.net-mvc-5

启用客户端验证后,为什么DropDownList或字符串属性未被验证客户端?使用EF 6.1,MVC 5 VS2012。

这是我的EF课程

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace SampleApp.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Business
    {
        public int BusinessKeyId { get; set; }
        public string BusinessName { get; set; }
        public string BusinessType { get; set; }
        public int ContactId { get; set; }

        public virtual Contact Contact { get; set; }
    }
}

我的控制器操作

// GET: /Business/
public ActionResult Index()
{
    var businesses = _db.Businesses.Include(b => b.Contact);
    return View(businesses.ToList());
}

// GET: /Business/Create
public ActionResult Create()
{
    ViewBag.ContactId = new SelectList(_db.Contacts, "ContactId", "Name");
    return View();
}

// POST: /Business/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include="BusinessKeyId,BusinessName,BusinessType,ContactId")] Business business)
{
    if (ModelState.IsValid)
    {
        _db.Businesses.Add(business);
        _db.SaveChanges();
        return RedirectToAction("Index");
    }

    ViewBag.ContactId = new SelectList(_db.Contacts, "ContactId", "Name", business.ContactId);
    return View(business);
}

和我的观点

@model SampleApp.Models.Business

@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="~/Scripts/jquery-2.1.0.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>


<h2>Create</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Business</h4>
        <hr />
        @Html.ValidationSummary(true)

        <div class="form-group">
            @Html.LabelFor(model => model.BusinessKeyId, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.BusinessKeyId)
                @Html.ValidationMessageFor(model => model.BusinessKeyId)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.BusinessName, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.BusinessName)
                @Html.ValidationMessageFor(model => model.BusinessName)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.BusinessType, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.BusinessType)
                @Html.ValidationMessageFor(model => model.BusinessType)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ContactId, "ContactId", new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("ContactId", String.Empty)
                @Html.ValidationMessageFor(model => model.ContactId)
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

以下是验证的片段,其中id仅经过验证

enter image description here

1 个答案:

答案 0 :(得分:2)

在您的课程中,您需要添加属性Required的模型。由于EF认为第一个是关键,这就是为什么你只有第一个: 试试这样:

   public partial class Business
   {
     [Key] //And this one should be the key 
     public int BusinessKeyId { get; set; }
     [Required]
     public string BusinessName { get; set; }
     [Required]
     public string BusinessType { get; set; }
     [Required]
     public int ContactId { get; set; }

    public virtual Contact Contact { get; set; }
   }

注意第一个属性BusinessKeyId您还可以让数据库生成该属性以避免重复。如果你想要,那么我可以编辑我的答案。但是现在我认为它应该有用。