asp.net mvc - MVC call action method from Html.DropDownList -


_layout:

@if (user.identity.isauthenticated)                         {                             <li>@html.dropdownlist("user", new list<selectlistitem>                            {                                new selectlistitem { text = user.identity.name, value = "1", selected = true },                                new selectlistitem { text = "logout", value = "2" }                            })</li>                         } 

when user clicks on logout option drop down list need call logout() method can actionlinks. how do this?

edit: signout isn't working new jquery code. why this?

public actionresult logout()         {             formsauthentication.signout();          return view("../home/index");     }  

my old code still works though logging user off though.

  <li>@html.actionlink("logout", "logout", "users", new { }, new { @class = "nav-link" })</li> 

you this, assuming using jquery:

<script type="text/javascript">     $(function() {         $('#user').on('change', function (event) {             if (event.currenttarget.selectedindex === 1) {                 // redirect logout controller, redirect                 window.location.href = '/controller/logoutaction';             }         });     }); </script> 

alternatively unordered list these items, displayed on click or hover of other element. of course you'd have style it. way use actionlink. mvc.authentication.logout() example action logout.

<div class="mydropdown">     <ul>         <li>user.identity.name</li>         <li>@html.actionlink("log out", mvc.authentication.logout())</li>     </ul> </div> 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -