asp.net - Passing a model through a view to another action without editing -
i've got form takes in lot of data. data stored in model object.
when form submitted, if passes validation, model passed confirmation view, displays of information submitted in form.
[httppost] public actionresult clientprofile(clientprofileformmodel model) { if (modelstate.isvalid) { return view("clientprofileconfirmation",model); } return view(model); }
when user clicks submit button @ bottom, need model come action can send email.
[httppost] public actionresult clientprofileconfirmationsubmit(clientprofileformmodel model) { string emailtoadminbody = generatefirmprofileadminemail(model); emaillogic.instance.sendemail("test@test.com", "test@test.com", "firm profile " + model.firmaddress.name, emailtoadminbody); return view(model); }
my problem is: need simple way of getting model httppost action of form (which sends confirmation page after validation) httppost action of confirmation. avoid filling confirmation view hidden inputs can bring in via form.
i've tried storing model in session , tempdata, both ways returning null reason. think it's going through multiple actions.
this shouldn't difficult! missing? way put in bunch of hidden input fields form of confirmation page?
i've used tempdata , it's working fine.
the model:
public class clientprofileformmodel { public string name { get; set; } public string address { get; set; } public string country { get; set; } }
the controller:
public class clientcontroller : controller { // public actionresult clientprofile() { return view(); } [httppost] public actionresult clientprofile(clientprofileformmodel model) { if (modelstate.isvalid) { return redirecttoaction("clientprofileconfirmation", model); } return view(model); } // public actionresult clientprofileconfirmation(clientprofileformmodel model) { return view(model); } [httppost] public actionresult clientprofileconfirmation() { var model = (clientprofileformmodel) tempdata["clientprofile"]; string emailtoadminbody = generatefirmprofileadminemail(model); emaillogic.instance.sendemail(...); tempdata["success-message"] = "your profile has been approved. check inbox."; return view("clientprofileconfirmation", model); } }
the clientprofile view
@model stackoverflow.models._31465719.clientprofileformmodel @{ viewbag.title = "clientprofile"; } <h2>clientprofile</h2> @using (html.beginform()) { @html.antiforgerytoken() <div class="form-horizontal"> <h4>clientprofileformmodel</h4> <hr /> @html.validationsummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @html.labelfor(model => model.name, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.name, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.name, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @html.labelfor(model => model.address, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.address, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.address, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @html.labelfor(model => model.country, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.country, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.country, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="create" class="btn btn-default" /> </div> </div> </div> }
the clientprofile confirmation view:
@model stackoverflow.models._31465719.clientprofileformmodel @{ tempdata["clientprofile"] = model; viewbag.title = "clientprofileconfirmation"; var successmessage = tempdata["success-message"]; } <h2>clientprofileconfirmation</h2> @if (successmessage != null) { <div class="alert alert-success"> <p class="text-success">@successmessage</p> </div> } @using (html.beginform()) { <div> <h4>clientprofileformmodel</h4> <hr /> <dl class="dl-horizontal"> <dt> @html.displaynamefor(model => model.name) </dt> <dd> @html.displayfor(model => model.name) </dd> <dt> @html.displaynamefor(model => model.address) </dt> <dd> @html.displayfor(model => model.address) </dd> <dt> @html.displaynamefor(model => model.country) </dt> <dd> @html.displayfor(model => model.country) </dd> </dl> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="confirm" class="btn btn-default" /> </div> </div> }
all did in last confirmation view save model in tempdata:
tempdata["clientprofile"] = model;
and read in controller:
var model = (clientprofileformmodel) tempdata["clientprofile"];
Comments
Post a Comment