asp.net mvc - Creating reusable forms with partial views -
i'm trying figure out best way create reusable forms mvc. situation have few forms used editing groups of data used on several places on site dont want copy paste same forms each of these places. i'm trying use partial views cannot make work , maybe i'm on wrong track here.
the case shows problem have 1 view 3 different tabs (all tabs in same view , using simple javascript). on each tab have added partial view editing specific data. have created partial views controller , everything. each partial view contains own form validation summary.
if controller actions return partialview result partial view rendered alone, without parent view surrounding it. doing wrong here if result?
am on wrong track this? there other solution should doing instead?
to more specific questions have:
1) there common pattern/solution implementing components include form?
2) using solution here partial views, how can render result of controller action inside master view way rendered before post?
parts of code master view:
<div class="tab-content no-border padding-24"> <div id="editprofile" class="tab-pane in active"> @{ html.renderaction("edituserprofile", "userdata", new { userid = model.id }); } </div> <div id="password" class="tab-pane"> @{ html.renderaction("editpassword", "userdata", new { userid = model.id }); } </div> </div>
the partial view editing password:
@model project.web.models.shared.editpasswordviewmodel <div id="summary"> @html.validationsummary(false) </div> <div class="row"> @using (html.beginform("editpassword", "userdata", formmethod.post, new { id = "editpasswordform", @class = "form-horizontal site-validation" })) { @html.antiforgerytoken() @html.hiddenfor(o => o.id) @html.passwordfor(o => o.password, new { @class = "col-xs-12 form-control", type = "password", id = "password-field" }) @html.textboxfor(o => o.newpassword, new { @class = "col-xs-12", type = "text", id = "new-password-field"}) @html.textboxfor(o => o.repeatnewpassword, new { @class = "col-xs-12", type = "password", id = "repeat-new-password-field"}) <button type="submit" class="width-35 pull-right btn btn-sm btn-primary"> <i class="icon-hdd"></i> save </button> } </div>
finally controller method save password , go same view:
[httppost] [validateantiforgerytoken] [authorize()] public partialviewresult editpassword(editpasswordviewmodel model) { try { if (modelstate.isvalid) { //get user user user = getuserbyid(model.id); //update user updatepassword(user.username, model.password, model.newpassword); } return partialview("_editpassword", model); } catch (domainexception ex) { modelstate.addmodelerror("", ex.message); return partialview("_editpassword", model); } }
thanks!
Comments
Post a Comment