非模型MVC下拉列表需要验证

时间:2016-11-23 00:38:35

标签: c# asp.net-mvc razor

我有一个来自Viewbag的下拉列表,我需要验证它像#34;必需"

我的控制器

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 })  

但没有人对我有用,如果我点击按钮而没有选择它会运行异常。我做错了什么?

2 个答案:

答案 0 :(得分:1)

将所需的内容发送为此类:

<强>更新

使用带有optionlabel的重载。我正在过去&#34;选择&#34;在这里,但你可以通过你需要的任何东西。还要提供验证摘要以通知用户它是必填字段。

@Html.ValidationSummary()
 @Html.DropDownList("name", new List<SelectListItem> { new SelectListItem { Text = "1", Value = "2" } }, "choose", htmlAttributes: new { @class = "form-control combo2 required", @data_val = "true", @data_val_required = "choose is a required field" })                    

答案 1 :(得分:0)

解决html和razor的情况很重要,主要是为了提高我的知识。正如我上面所说,我可以找到一个使用Js和名为Sweet Alert的组件的解决方案如下:

var data1 = $('#data1').val();
var data2 = $('#data2').val();
var cliente = $('#IdCliente option:selected').val();

if (cliente != "") {

            $.ajax({
                url: '',
                dataType: "json",
                type: "GET",
                data: { 'data1': data1, 'data2': data2, 'cliente': cliente },
                success: function (data) {
                    }
                },

                error: function (xhr) {
                    alert('error');
                }
            });
        }
        else {
            swal({ type: "warning", title: "Alerta", text: "Selecione uma empresa por favor!", timer: 5000, showConfirmButton: true });
        }

结果如下:

Alert Sweet