我使用数据库优先方法创建了一个项目,其中我的表
城市-包含以下列
City_Id
City_Name
State_Id
State_Id是状态表的外键。
现在,所有验证和所有操作均正常进行,直到我在City.cs模型文件中的State_Id上放置[Required]批注,这是
//------------------------------------------------------------------------------
// <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 SS.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
public partial class City
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public City()
{
this.Areas = new HashSet<Area>();
}
public int City_Id { get; set; }
[Required]
[DisplayName("City Name")]
[StringLength(50,ErrorMessage = "City Name exceeds limit of 50 characters")]
[RegularExpression(@"[A-Z][a-z]+",ErrorMessage ="Input Format Incorrect")]
[Remote("CheckDuplicateInState","Cities",AdditionalFields ="State_Id",ErrorMessage ="There cannot be duplicate city names in same state")]
public string City_Name { get; set; }
[DisplayName("State Name")]
public int State_Id { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Area> Areas { get; set; }
public virtual State State { get; set; }
}
}
当我在State_Id上放置[Required]并在Create视图中的表单中按下按钮Submit时,什么都不会发生,即使我从下拉列表中选择了正确的状态也不会发生回发,或者我只是选择了默认的显示值“ Select”状态”,则也不会显示任何错误。
我的看法如下
@model SS.Models.City
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>City</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.City_Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.City_Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.City_Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.State_Id,htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("State_Id", null,"Select State", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.State_Id, "", new { @class = "text-danger" })
</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")
}
我的控制器(相关代码)如下
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using SS.Models;
namespace SS.Controllers.Admin
{
public class CitiesController : Controller
{
private SS db = new SSDatabaseEntities();
// GET: Cities/Create
public ActionResult Create()
{
ViewBag.State_Id = new SelectList(db.States, "State_Id", "State_Name");
return View();
}
// POST: Cities/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "City_Id,City_Name,State_Id")] City city)
{
if (ModelState.IsValid)
{
db.Cities.Add(city);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(city);
}
public JsonResult CheckDuplicateInState(string City_Name,int state_id)
{
State st = db.States.Find(state_id);
if (st.Cities.Any(x => x.City_Name == City_Name))
return Json(false, JsonRequestBehavior.AllowGet);
else
return Json(true, JsonRequestBehavior.AllowGet);
}
}
}
将[Required]放在City.cs的State_Id上时,在查看页面源代码的下拉html中不会生成data-val-required的必需属性。
<div class="form-group">
<label class="control-label col-md-2" for="State_Id">State Name</label>
<div class="col-md-10">
<select class="form-control" id="State_Id" name="State_Id"><option value="">Select State</option>
<option value="1">Madhya Pradesh</option>
<option value="2">Gujarat</option>
<option value="4">Punjab</option>
<option value="9">Tamil Nadu</option>
<option value="10">Haryana</option>
</select>
<span class="field-validation-valid text-danger" data-valmsg-for="State_Id" data-valmsg-replace="true"></span>
</div>
</div>
您对的使用@Html.DropDownList("State_Id", null, ...)
绑定到,State_Id
后者是SelectList
您添加到的属性ViewBag
(并且没有任何验证属性)。
而是为属性赋予您的绑定SelectList
名称和其他名称,以便您可以牢固地绑定至模型属性。在GET方法中
ViewBag.StateList = new SelectList(db.States, "State_Id", "State_Name");
并认为
@Html.DropDownListFor(m => m.State_Id, (SelectList)ViewBag.StateList, "Select State", new { @class = "form-control" })
现在将添加正确的data-val-required
和data-val-number
属性,以进行客户端验证。
然后在POST方法中,您需要重新填充ViewBag
属性,然后再返回视图
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(City city)
{
if (ModelState.IsValid)
{
....
}
ViewBag.StateList = new SelectList(db.States, "State_Id", "State_Name"); // add this
return View(city);
}
旁注:不需要[Bind]
属性,因为您包含的所有属性都是默认属性(并且您包含的属性City_Id
没有意义,因为您没有-也不应拥有对此控件的控制)。
更好的方法是使用包含以下内容的视图模型,IEnumerable<SelectListItem> StateList
以便您可以简单地使用@Html.DropDownListFor(m => m.State_Id, Model.StateList, ....)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句