asp.net mvc - MVC4 Razor Multiple strongly type partial view with action in one page -
i have requirement have login , register form in homepage. believe quite common scenario having difficulty achieving this.
this login , register forms 2 separate type partial view being used in index view
below controller register. skip login since if work, other should similar.
register controller
// // get: /account/register [allowanonymous] public actionresult register() { return partialview(); } // // post: /account/register [httppost] [allowanonymous] [validateantiforgerytoken] public actionresult register(registermodel registermodel) { if (modelstate.isvalid) { // attempt register user try { _websecurity.createuserandaccount(registermodel.email, registermodel.password, new { registermodel.firstname, registermodel.lastname, registermodel.email }); _websecurity.login(registermodel.email, registermodel.password); return redirecttoaction("manage", "account"); } catch (membershipcreateuserexception e) { modelstate.addmodelerror("", errorcodetostring(e.statuscode)); } } // if got far, failed, redisplay form return view(registermodel); }
index controller
// // get: /home/ public actionresult index() { // // if logedin redirect profile page // else show home page view // viewbag.message = "modify template jump-start asp.net mvc application."; if (request.isauthenticated) { return redirecttoaction("manage", "account", new { id = httpcontext.user.identity.name }); } return view(); }
register view
@using system.web.optimization @model boilku.web.viewmodels.registermodel @using (html.beginform("register","account", formmethod.post)) { @html.antiforgerytoken() @html.validationsummary() <fieldset> <legend>registration form</legend> <ol> <li> @html.labelfor(m => m.firstname) @html.textboxfor(m => m.firstname) </li> ... ... omitted codes ... <li> @html.labelfor(m => m.password) @html.passwordfor(m => m.password) </li> </ol> <input type="submit" value="register" /> </fieldset> } @section scripts { @scripts.render("~/bundles/jqueryval") }
index view
@model boilku.web.viewmodels.homemodel @{ viewbag.title = "home page"; } @{ html.renderaction("login", "account"); } @{ html.renderaction("register", "account"); }
now above code, have managed partial view display on homepage. when click "register" after filling details automatically redirect register page fields pre populated. not want. expect register happened on homepage , redirect profile page when has registered. how go doing this? thank reading , apology noobishe questions. still quite new mvc.
update changing register controller return partialview() view() act according above requirement. embed page page. (i.e. top navigation duplicated. ) anyone?
i think better create partial page (user control) register view
_register.cshtml
(no change here, removed script section. parent view render necessary script)
@using system.web.optimization @model boilku.web.viewmodels.registermodel @using (html.beginform("register","account", formmethod.post)) { @html.antiforgerytoken() @html.validationsummary() <fieldset> <legend>registration form</legend> <ol> <li> @html.labelfor(m => m.firstname) @html.textboxfor(m => m.firstname) </li> ... ... omitted codes ... <li> @html.labelfor(m => m.password) @html.passwordfor(m => m.password) </li> </ol> <input type="submit" value="register" /> </fieldset> }
index.cshtml
inside homemodel, needs have registermodel property
@model boilku.web.viewmodels.homemodel @{ viewbag.title = "home page"; } @{ html.renderaction("login", "account"); } @{ @html.partial("register", model.registermodel) }
when create mvc application, retrieve information required page , put model. in scenario, homemodel contains registermodel details. inside view, can render partial page , pass model argument. partial view doesn't have go controller.
then in register page, can partial render
@html.partial("register", model.registermodel)
Comments
Post a Comment