Tuesday, 15 January 2013

c# - Pass model from partial view to controller -



c# - Pass model from partial view to controller -

so partial view looks -

@model example.models.forgotpasswordviewmodel @html.validationsummary("", new { @class = "text-danger" }) <div class="form-group"> @html.labelfor(m => m.email, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @html.textboxfor(m => m.email, new { @class = "form-control" }) </div> </div> <div style="font-size: 16px; text-align:center; margin-top:10px;"> @html.actionlink("email link", "forgotpassword", "account") </div>

as part of asp.net's out of box forgotpassword view, turned partial view. original view wrapped in "html.beginform(..", can't that. (it results in form within form)

how phone call business relationship controller's forgotpassword method expects model passing declared @model?

html generated @html.textboxfor(m => m.email, new { @class = "form-control" }) :

<input class="form-control" id="email" name="email" type="text" value="" />

signature accountcontroller.forgetpassword(..) :

// // post: /account/forgotpassword [httppost] [allowanonymous] [validateantiforgerytoken] public async task<actionresult> forgotpassword(forgotpasswordviewmodel model) { if (modelstate.isvalid) { var user = await usermanager.findbynameasync(model.email); if (user == null || !(await usermanager.isemailconfirmedasync(user.id))) { // don't reveal user not exist or not confirmed homecoming view("forgotpasswordconfirmation"); } // more info on how enable business relationship confirmation , password reset please visit http://go.microsoft.com/fwlink/?linkid=320771 // send email link string code = await usermanager.generatepasswordresettokenasync(user.id); var callbackurl = url.action( "resetpassword", "account", new { userid = user.id, code = code }, protocol: request.url.scheme); await usermanager.sendemailasync(user.id, "reset password", "please reset password clicking <a href=\"" + callbackurl + "\">here</a>"); homecoming view("forgotpasswordconfirmation"); } // if got far, failed, redisplay form homecoming redirecttoaction("index", "home"); }

also erik pointed out, need create sure fields uniquely identified within partial view. post had email twice.

@erikphilips how utilize jquery alter form's destination? :)

i alter action link button:

@html.validationsummary("", new { @class = "text-danger" }) <div class="form-group"> @html.labelfor(m => m.email, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @html.textboxfor(m => m.email, new { @class = "form-control" }) </div> </div> <div style="font-size: 16px; text-align:center; margin-top:10px;"> <input type="button" value="email link" class="change-form-url" data-url="@url.action("forgotpassword", "account")"/> </div>

jquery:

$(document).ready(funciton() { $('.change-form-url').on('click', function() { var url = $(this).data('url'); var form = $(this).closest('form'); form.prop('action', url); form.submit(); }); });

i have not tested this, should close want.

c# asp.net-mvc razor asp.net-mvc-5

No comments:

Post a Comment