【问题标题】:MVC 5 DropDownList Client ValidationMVC 5 DropDownList 客户端验证
【发布时间】:2014-05-11 04:42:32
【问题描述】:

启用客户端验证后,为什么 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")
}

这是验证的 sn-p,其中仅验证 id

【问题讨论】:

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


    【解决方案1】:

    在你的类中,你需要的模型添加属性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 的第一个属性,您也可以让数据库生成该属性以避免重复。如果你想问,我可以编辑我的答案。但现在我认为它应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-29
      • 2015-09-05
      • 2010-09-14
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多