我有一個Viewbag一個下拉列表,我需要對其進行驗證,如 「必需」驗證所需的非型號MVC DROPDOWNLIST
我的控制器
public ActionResult EsperaPorHora()
{
var cliente = new UsuarioData().Id_LicenciadoPorId(User.Identity.GetUserId());
var Cli = !string.IsNullOrEmpty(cliente.ToString()) ? Convert.ToInt32(cliente) : 0;
var cliData = new LicenciadoData();
var agora = DateTime.Now;
ViewBag.Data1 = agora.ToShortDateString();
ViewBag.Data2 = agora.ToShortDateString();
if (Cli != 0)
{
ViewBag.IdCliente = new SelectList(cliData.ListaClienteId(Cli), "Id", "Fantasia");
}
else
{
ViewBag.IdCliente = new SelectList(cliData.ListarClientes(), "Id", "Fantasia");
}
return View();
}
[HttpGet]
public JsonResult EsperaHora(string data1, string data2, int? cliente)
{
var voiceData = new KiperVoiceData(cliente);
var media = voiceData.GetEsperaData(data1, data2);
var atend = voiceData.GetEsperaHora(data1, data2);
var result = new { atend, media };
return Json(result, JsonRequestBehavior.AllowGet);
}
我想:
@Html.DropDownList("IdCliente", null, "SELECIONE A EMPRESA", htmlAttributes: new { @class = "form-control combo2", @required = "required" })
@Html.DropDownList("IdCliente", null, "SELECIONE A EMPRESA", htmlAttributes: new { @class = "form-control combo2", @required = true })
@Html.DropDownList("IdCliente", null, "SELECIONE A EMPRESA", htmlAttributes: new { @class = "form-control combo2", required = true })
但沒有人工作給我,如果我點擊按鈕沒有選擇它運行到一個例外。什麼即時做錯了?
如果你不綁定到你不能得到驗證一個模型!。你需要綁定到一個模型屬性(帶有[[Required]'屬性,並且'SelectList'的名字不能和你綁定的屬性名稱相同。然後POST方法中的參數需要是模型 –
我的下拉列表是從列表中填充的,我如何將它綁定到模型?newbie here ...我可以做一個js驗證嗎? –
'@ Html.DropDownListFor(m> m.PropertyToBindTo,Model.YouSelectList ,「SELECIONE A EMPRESA」,新的{@class =「form-control combo2」})' –