DropDownListFor只有在另一个必需字段验证触发时选择了optionLabel才会触发必需的验证

时间:2016-11-02 12:08:15

标签: c# asp.net-mvc

我有一个包含2个文本框和2个下拉列表的页面。

下拉列表中包含optionLabels,我希望所需的验证显示是否已选中这些。

其中一个文本框和两个下拉列表都是'必需'。

如果我填写了所需的文本框并提交了表单,则需要'下拉列表中的验证不会触发,只有在“必需”的情况下才会触发。文本框没有值。

模型

public class CreateCaseModel
    {
        public CaseRegModel CaseDetails { get; set; }
        public List<StartTypeListModel> CaseStartTypeList { get; set; }
        public List<ClientListModel> ClientsList { get; set; }
    }


public class CaseRegModel
    {
        [Key]
        public int CaseId { get; set; }
        [Required]
        [Display(Name = "PX Request Date")]
        public DateTime PxRequestDate { get; set; }
        [Required]
        public int ClientId { get; set; }
        [Required]
        [Display(Name = "Start Type")]
        public string StartType { get; set; }
        [Display(Name = "List Price")]
        public string ListPrice { get; set; }

    }

    public class ClientListModel
    {
        [Required]
        public int ClientId { get; set; }

        [Display(Name = "Client Name")]
        public string ClientName { get; set; }
    }

    public class StartTypeListModel
    {
        public int Id { get; set; }

        [Required]
        [Display(Name = "Start Type")]
        public string StartType { get; set; }
    }

VIEW - CreateCaseModel

<div class="form-group">
                @Html.LabelFor(model => model.CaseDetails.StartType, new { @class = "control-label col-md-4" })
                <div class="col-md-6">
                    @*@Html.DropDownList("StartType", null, "Select Start Type...", new { @class = "form-control" })*@
                    @Html.DropDownListFor(model => model.CaseDetails.StartType, new SelectList(Model.CaseStartTypeList, "StartType", "StartType"), "Select Start Type...", new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.CaseDetails.StartType, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.CaseDetails.PxRequestDate, htmlAttributes: new { @class = "control-label col-md-4" })
                <div class="col-md-6">
                    @Html.EditorFor(model => model.CaseDetails.PxRequestDate, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.CaseDetails.PxRequestDate, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="col-md-6">

            <div class="form-group">
                @Html.LabelFor(model => model.CaseDetails.ClientId, new { @class = "control-label col-md-4" })
                <div class="col-md-6">
                    @*@Html.DropDownList("ClientId", null, "Select Client...", new { @class = "form-control" })*@
                    @Html.DropDownListFor(model => model.CaseDetails.ClientId, new SelectList(Model.ClientsList, "ClientId", "ClientName"), "Select Client...", new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.CaseDetails.ClientId, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.CaseDetails.ListPrice, htmlAttributes: new { @class = "control-label col-md-4" })
                <div class="col-md-6">
                    @Html.EditorFor(model => model.CaseDetails.ListPrice, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.CaseDetails.ListPrice, "", new { @class = "text-danger" })
                </div>
            </div>

CONTROLLER

// GET: Case/Create
        public ActionResult Index()
        {

            //start type list
            //var CategoryList = new List<string>();
            var qStartTypes = (from st in efContext.CaseStartTypes
                             orderby st.Id
                             select new StartTypeListModel
                             {
                                 Id = st.Id,
                                 StartType = st.StartType
                             }).ToList();


            //client list
            //var CategoryList = new List<string>();
            var qClients = (from c in efContext.PgsClients
                               orderby c.ClientName
                               select new ClientListModel
                               {
                                   ClientId = c.ClientId,
                                   ClientName = c.ClientName
                               }).ToList();


            return View(new CreateCaseModel
            {
                CaseDetails = new CaseRegModel(),
                CaseStartTypeList = qStartTypes,
                ClientsList = qClients
            });
        }

感谢您的帮助。

0 个答案:

没有答案