必需属性不适用于外键下拉列表

莫希特·沙(Mohit Shah)

我使用数据库优先方法创建了一个项目,其中我的表

城市-包含以下列

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-requireddata-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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

选择不适用于默认情况下不显示的选择下拉列表

来自分类Dev

Select2 allowClear不适用于动态下拉列表

来自分类Dev

填充属性不适用于SVG

来自分类Dev

Bootstrap下拉列表不适用于输入组

来自分类Dev

AngularJS必需的属性不适用于选择

来自分类Dev

必填字段验证器不适用于数据绑定下拉列表

来自分类Dev

ui-bootstrap下拉列表似乎不适用于版本0.12.0

来自分类Dev

prop()不适用于某些属性

来自分类Dev

JsonConstructor属性不适用于RavenDB

来自分类Dev

Kendo网格-分组不适用于自定义编辑器下拉列表

来自分类Dev

HTML5必需的属性不适用于AJAX提交

来自分类Dev

翻译属性不适用于div

来自分类Dev

Expo SQLite不适用于带有外键的表

来自分类Dev

导航下拉列表不适用于timeOuts

来自分类Dev

填充属性不适用于SVG

来自分类Dev

拆分不适用于属性

来自分类Dev

动态填充下拉列表的Ajax调用适用于FF,但不适用于Chrome和IE

来自分类Dev

JsonConstructor属性不适用于RavenDB

来自分类Dev

jQuery克隆验证不适用于HTML下拉列表

来自分类Dev

jQuery验证不适用于多选下拉列表

来自分类Dev

NG必需,不适用于搜索输入字段

来自分类Dev

领域查询不适用于列表

来自分类Dev

SelectNextControl 不适用于 Telerik 下拉列表

来自分类Dev

css 属性不适用于 div

来自分类Dev

为什么边框属性不适用于列表项?

来自分类Dev

HTML 必需标签不适用于公司和名称

来自分类Dev

keyof 不适用于“名称”属性

来自分类Dev

Class="dropdown" 和属性数据下拉菜单不适用于嵌套的 ul

来自分类Dev

材料 ui TextField 中的必需属性不适用于提交