我使用数据库第一种方法创建了一个项目,其中我的表
城市 - 有以下列
userSessions.js
State_Id是状态表的外键。
现在所有验证和所有内容都有效,直到我将State_Id上的[Required]注释放在我的City.cs模型文件中,这是
City_Id
City_Name
State_Id
当我把[Required]放在State_Id上并按下Create窗口中的提交按钮时,没有任何反应,即使我从下拉列表中选择了正确的状态也没有回发,或者我只选择默认的显示值"选择州"然后也没有显示错误。
我的观点如下
//------------------------------------------------------------------------------
// <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; }
}
}
我的控制器(相关代码)如下
@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")
}
在City.cs中将[Required]放在State_Id上时,在查看页面源的下拉html中不会生成所需的属性data-val-required。
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);
}
}
}
答案 0 :(得分:3)
您对@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, ....)