asp.net mvc - MVC - display a user profile setting in dropdown list on several pages -
i display value of specific user profile setting in header of every page (_layout.cshtml). see how default mvc web application displays user's name in "welcome user!", name obtained calling user.identity.getusername()
. setting want display custom setting.
it seem need pass custom setting controller, i'm not sure how controller within _layout.cshtml. i've tried @html.renderpartial, doesn't trigger controller action/method, or can it?
i've tried @html.renderaction gets me controller, requires entire view rendered, or there way around this?
could tell me razor syntax use view trigger controller action render input control? don't need controller/action logic other return statement.
edit
@rowan freeman seems pointing me in direction tried. in fairness didn't know need perform post user setting. here code i've tried:
viewmodel
public class languagelistpartialviewmodel { public string selectedlanguage { get; set; } public ienumerable<system.web.mvc.selectlistitem> languages { get; set; } }
~/views/shared/_languagelistpartial.cshtml
@model gps_web_app.models.languagelistpartialviewmodel @html.dropdownlistfor( x => x.selectedlanguage, model.languages, new { @class = "form-control", onchange = "this.form.submit();" } )
~/views/shared/_layout.cshtml
<form action="/account/languagelistpartial" method="post"> @{html.renderaction("languagelistpartial", "account");} </form>
get controller action
[allowanonymous] [childactiononly] public actionresult languagelistpartial() { // logic create populate model's list<selectlistitem> languages // logic set model's string selectedlanguage // logic written , returns correct list. return partialview("_languagelistpartial", model); }
post controller action
[allowanonymous] [httppost] public async task<actionresult> languagelistpartial(string selectedlanguage) { // logic set user profile setting selection // logic written , correctly sets user profile setting return redirecttolocal(request.urlreferrer.absolutepath); }
the reason wrap partial view in _layout.cshtml because there times when want display list within form on different page - example, editprofile page - contains it's own wrapper. have 1 caveat, partialview has onsubmit() has undesirable effect of submitting form it's contained it. i'll fix later.
the problem above layout seems return() in post action - rendered page on return consists of dropdown list instead of rendering dropdown list within page called from.
use html.action
in conjunction partial view.
you're close solution. let me clarify steps.
- create partial view. partial view regular view they're small , used part of final page rendered. partial view might called "_userprofile.cshtml". underscore @ beginning convention indicating partial view. view should use simple model, perhaps
userprofilemodel
. - create appropriate controller , action. controller called
partialscontroller
, action mightuserprofile()
. attach attribute[childactiononly]
action (or controller) can't accessed regular url. action shouldreturn partialview("_userprofile");
. - call action
_layout
using@html.action("userprofile", "partials")
.
Comments
Post a Comment