我有一个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”})' –