user3740906
user3740906

Reputation: 1

as I can I know the value [email protected] (....) selected in the view? in MVC

I need to know which item is selected from dropdownlist in view how do I know from @Html.DropDownList (....) because I need to use the value "text" of dropdownlist in a query "if (...)"

example:

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

Upvotes: 0

Views: 164

Answers (2)

Shekhar Pankaj
Shekhar Pankaj

Reputation: 9145

<span>@Html.DropDownList("Value", ViewBag.ComboEnviar as IEnumerable<SelectListItem>, "Seleccione una opción")

when you will check in DeveloperTool you will find it will render as

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

so it is Clear you can use its Id="Value" as JQUERY Selector

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

Still Confusion!! Comment Below it

Upvotes: 1

tvanfosson
tvanfosson

Reputation: 532765

Not sure exactly what you're trying to do but you certainly don't want to perform the check on the result of the DropDownList method. You probably want to find the selected item in the ViewBag.ComboEnviar. Note this will only work when the page is rendered (server-side), it won't do anything client-side if the value is changed. For that you'd need some 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>
}

Upvotes: 0

Related Questions