【问题标题】:as I can I know the value del@Html.DropDownList (....) selected in the view? in MVC我知道在视图中选择的值 del@Html.DropDownList (....) 吗?在 MVC 中
【发布时间】:2014-06-14 18:37:37
【问题描述】:

我需要知道从下拉列表中选择了哪个项目 我怎么知道@Html.DropDownList (....) 因为我需要在查询“if (...)”中使用下拉列表的值“文本”

示例:

 <div class="editor-label">
    <span>Enviar una Notificacion a...</span><br />
    <span>@Html.DropDownList("Value", ViewBag.ComboEnviar as IEnumerable<SelectListItem>, "Seleccione una opción")</span>

 </div>

....

 @if (@Html.DropDownList(...).SelectValue == 0)
 {
      <label for="descripcion">@Html.LabelFor(model => model.nameUsuario)</label>
 }

【问题讨论】:

    标签: asp.net-mvc html-select


    【解决方案1】:
    <span>@Html.DropDownList("Value", ViewBag.ComboEnviar as IEnumerable<SelectListItem>, "Seleccione una opción")
    

    当您签入 DeveloperTool 时,您会发现它会呈现为

    <select id="Value" name="Value">...</select>
    

    所以很明显,您可以将其Id="Value" 用作JQUERY Selector

    if($('#Value').val()==0)
    {
     //Do some Magic Stuff Here
    }
    

    还是一头雾水!!在下方评论

    【讨论】:

      【解决方案2】:

      不确定您到底要做什么,但您肯定不想对DropDownList 方法的结果进行检查。您可能希望在ViewBag.ComboEnviar 中找到所选项目。请注意,这仅在呈现页面(服务器端)时有效,如果值更改,它将不会在客户端执行任何操作。为此,您需要一些 JavaScript。

      @{
          var selectList = ((IEnumerable<SelectListItem>)ViewBag.ComboEnviar).ToList();
          var selectedItem = selectList.FirstOrDefault(i => i.Selected && Value == 0);
      }
      
      
      <div class="editor-label">
          <span>Enviar una Notificacion a...</span><br />
          <span>@Html.DropDownList("Value", selectList, "Seleccione una opción")</span>    
      </div>
      
      @if (selectedItem != null)
      {
          <label for="descripcion">@Html.LabelFor(model => model.nameUsuario)</label>
      }
      

      【讨论】:

        猜你喜欢
        • 2015-03-13
        • 1970-01-01
        • 2011-04-14
        • 2013-03-25
        • 1970-01-01
        • 2021-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多