razor view page setup with multiple sections -
i looked through various tutorials, sense confused on how want go executing have in mind , want help clarify thoughts.
i have razor view page called profile
; on profile page have 4 different sections:
i thinking of creating each of 4 sections partial section , each section have text boxes , save button allow user add together or alter information.
@model projects.models.passwordmodel @{ viewbag.title = "profile account"; } <hgroup class="title"> <h1>@viewbag.title.</h1> </hgroup> <p class="message-success">@viewbag.statusmessage</p> <div id="wrap"> <div id="right"> @if (viewbag.haslocalpassword) { @html.partial("_changepasswordpartial") } else { @html.partial("_setpasswordpartial") } </div> <div id="left"> @html.partial("_usernamepartial") @html.partial("_biographypartial") </div> </div>
how can set main view page , have 4 sections displayed , functional? got them displayed, when seek save alter made username error different model--say biography model. sense beingness connected when shouldn't. looking clear tutorial of how go , able create 1 after figure out.
this username partial
@model project.models.usernamemodel @using (html.beginform("_usernamepartial", "account")) { @html.antiforgerytoken() @html.validationsummary() <p>username</p> @html.textboxfor(m=>m.username) <button class="btn btn-small" type="submit" value="save username">save</button> }
my controller
get:
public actionresult _usernamepartial() { var usernamemodel = new usernamemodel(); using (var db = new datacontext()) { usernamemodel.nickname = (from u in db.users u.id == websecurity.currentuserid select u.username).firstordefault(); } homecoming view(usernamemodel); }
post:
[httppost] public actionresult _usernamepartial(usernamemodel usernamemodel, string returnurl) { if (modelstate.isvalid) { using (var db = new datacontext()) { user user = db.users.firstordefault(m => m.id == websecurity.currentuserid); user.username = usernamemodel.username; db.savechanges(); } homecoming redirecttoaction("_usernamepartial"); } homecoming view(returnurl); }
your help appreciated.
you need create sure views typed right model. view typed passwordmodel
model, appears 1 section of page. if create model composed of models each section, create easier:
class profilemodel { passwordmodel password { get; set; } biographymodel biography { get; set; } ... }
you set profile view typed profilemodel
. can pass each model partials:
@html.partial("_biographypartial",model.biography)
and illustration _biographypartial like:
@model projects.models.biographymodel ... <h1>@model.somebiographyproperty</h2>
you should careful check validation, assuming have some, working correctly set up.
alternatively, set of info 1 flat model , pass whole model each partial view.
edit: partial views aren't backed controllers , actions, they're views rendered using info pass them calling view. you're trying right render _usernamepartial
action. if want can seek using action
instead of partial
:
<div id="left"> @html.action("_usernamepartial") ... </div>
and render action partial view:
public actionresult _usernamepartial() { var usernamemodel = new usernamemodel(); using (var db = new datacontext()) { usernamemodel.nickname = (from u in db.users u.id == websecurity.currentuserid select u.username).firstordefault(); } homecoming partialview(usernamemodel); }
razor view asp.net-mvc-4
No comments:
Post a Comment