Created
January 23, 2015 17:23
-
-
Save cpoDesign/bc9c5980a89cfe7b0caf to your computer and use it in GitHub Desktop.
Form wizard mvc with partial views with
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @model FormWizardInMvc.Controllers.Contact | |
| @{ | |
| Layout = null; | |
| } | |
| <div class="form-horizontal"> | |
| <div class="form-group"> | |
| @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" }) | |
| <div class="col-md-10"> | |
| @Html.TextBoxFor(model => model.Email, new { data_toggle = "tooltip" }) | |
| @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" }) | |
| </div> | |
| </div> | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @model FormWizardInMvc.Controllers.Location | |
| @{ | |
| Layout = null; | |
| } | |
| <div class="form-horizontal"> | |
| <div class="form-group"> | |
| @Html.LabelFor(model => model.Postcode, htmlAttributes: new { @class = "control-label col-md-2" }) | |
| <div class="col-md-10"> | |
| @Html.TextBoxFor(model => model.Postcode, new { htmlAttributes = new { @class = "form-control" } }) | |
| @Html.ValidationMessageFor(model => model.Postcode, "", new { @class = "text-danger" }) | |
| </div> | |
| </div> | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @{ | |
| ViewBag.Title = "CreateQuote"; | |
| Layout = "~/Views/Shared/_Layout.cshtml"; | |
| } | |
| <h2>CreateQuote</h2> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @model FormWizardInMvc.Controllers.FormSteps | |
| @{ | |
| ViewBag.Title = "Index"; | |
| Layout = "~/Views/Shared/_Layout.cshtml"; | |
| } | |
| <script type="text/javascript" src="/Scripts/jquery-1.10.2.min.js"></script> | |
| <script type="text/javascript" src="/Scripts/jquery.validate.min.js"></script> | |
| <script type="text/javascript" src="/Scripts/jquery.validate.unobtrusive.min.js"></script> | |
| <script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script> | |
| <script> | |
| $.validator.setDefaults({ | |
| showErrors: function (errorMap, errorList) { | |
| this.defaultShowErrors(); | |
| // destroy tooltips on valid elements | |
| $("." + this.settings.validClass) | |
| .tooltip("destroy"); | |
| // add/update tooltips | |
| for (var i = 0; i < errorList.length; i++) { | |
| var error = errorList[i]; | |
| $("#" + error.element.id) | |
| .tooltip({ trigger: "focus" }) | |
| .attr("data-original-title", error.message); | |
| } | |
| } | |
| }); | |
| </script> | |
| <h2>Index</h2> | |
| @using (Html.BeginForm("Step2", "Test", FormMethod.Post, null)) | |
| { | |
| @Html.ValidationSummary(false) | |
| <div class="form-horizontal"> | |
| <h4>FormSteps</h4> | |
| @Html.Partial("_Contact", Model.Contact, new ViewDataDictionary() | |
| { | |
| TemplateInfo = new TemplateInfo() | |
| { | |
| HtmlFieldPrefix = "Contact" | |
| } | |
| }) | |
| <hr /> | |
| @{ Html.EnableUnobtrusiveJavaScript(false);} | |
| @Html.Partial("_Location", Model.Location, new ViewDataDictionary() | |
| { | |
| TemplateInfo = new TemplateInfo() | |
| { | |
| HtmlFieldPrefix = "Location" | |
| } | |
| }) | |
| @{ Html.EnableUnobtrusiveJavaScript(true);} | |
| <hr /> | |
| <div class="form-group"> | |
| <div class="col-md-offset-2 col-md-10"> | |
| <input type="submit" value="Save" class="btn btn-default" /> | |
| </div> | |
| </div> | |
| </div> | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @model FormWizardInMvc.Controllers.FormSteps | |
| @{ | |
| ViewBag.Title = "Step2"; | |
| Layout = "~/Views/Shared/_Layout.cshtml"; | |
| } | |
| <h2>Step2</h2> | |
| <script src="~/Scripts/jquery-1.10.2.min.js"></script> | |
| <script> | |
| function myFunction() { | |
| $("#Requote").attr("checked", "checked"); | |
| $("form").validate().cancelSubmit = true; | |
| } | |
| </script> | |
| @using (Html.BeginForm("CreateQuote", "Test", FormMethod.Post, null)) | |
| { | |
| @Html.ValidationSummary(false, "", new { @class = "text-danger" }) | |
| <div class="form-group"> | |
| <div class="form-horizontal"> | |
| <h4>FormSteps</h4> | |
| @Html.Partial("_Contact", Model.Contact, new ViewDataDictionary() | |
| { | |
| TemplateInfo = new TemplateInfo() | |
| { | |
| HtmlFieldPrefix = "Contact" | |
| } | |
| }) | |
| <hr /> | |
| @Html.Partial("_Location", Model.Location, new ViewDataDictionary() | |
| { | |
| TemplateInfo = new TemplateInfo() | |
| { | |
| HtmlFieldPrefix = "Location" | |
| } | |
| }) | |
| <hr /> | |
| @Html.CheckBoxFor(x => x.Requote) | |
| <div class="col-md-offset-2 col-md-10"> | |
| <input type="submit" value="Save" class="btn btn-default" /> | |
| <button onclick="myFunction()">Start over</button> | |
| </div> | |
| </div> | |
| </div> | |
| } | |
| <div> | |
| @Html.ActionLink("Back to List", "Index") | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Linq; | |
| using System.Web.Mvc; | |
| namespace FormWizardInMvc.Controllers | |
| { | |
| using System.ComponentModel.DataAnnotations; | |
| public class TestController : Controller | |
| { | |
| // GET: Test | |
| public ActionResult Index() | |
| { | |
| var model = new FormSteps(); | |
| return View(model); | |
| } | |
| [HttpPost] | |
| // GET: Test/Create | |
| public ActionResult Step2(FormSteps model) | |
| { | |
| if (model.Requote) | |
| { | |
| return View("Index", model); | |
| } | |
| const string modelPrefix = "Location."; | |
| if (ModelState.Keys.Any(x => x.StartsWith(modelPrefix))) | |
| { | |
| foreach (var key in ModelState.Keys.Where(x => x.StartsWith(modelPrefix))) | |
| { | |
| ModelState[key].Errors.Clear(); | |
| } | |
| } | |
| if (ModelState.IsValid) | |
| { | |
| return View(model); | |
| } | |
| return View("Index", model); | |
| } | |
| [HttpPost] | |
| public ActionResult CreateQuote(FormSteps model) | |
| { | |
| if (model.Requote) | |
| { | |
| return View("Index", model); | |
| } | |
| if (ModelState.IsValid) | |
| { | |
| return View(model); | |
| } | |
| return View("Step2", model); | |
| } | |
| } | |
| public class FormSteps | |
| { | |
| public bool Requote { get; set; } | |
| public Contact Contact { get; set; } | |
| [Required] | |
| public Location Location { get; set; } | |
| } | |
| public class Location | |
| { | |
| [Required(ErrorMessage = "Please provide postcode")] | |
| public string Postcode { get; set; } | |
| } | |
| public class Contact | |
| { | |
| [Required(ErrorMessage = "Please provide email")] | |
| public string Email { get; set; } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment