Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC Razor - How to submit form to itself
    text
    copied!<p>I'm using MVC and Razor for the first time and have what is hopefully a really easy question. I have two pages on the site. The first page (Page A) has a very small form on it which has an input for an email address. when the user enters their email address and clicks submit they are sent to page B using HttpPost, like so:</p> <pre><code>@using (Html.BeginForm("NewsletterSignup","Common", FormMethod.Post)) { &lt;p&gt; &lt;input type="text" class="text" value="Enter your email address" id="email" name="email" /&gt; &lt;input type="submit" class="submit" value="Sign Up" /&gt; &lt;/p&gt; } </code></pre> <p>On Page B there is the main form which allows the user to also enter their name and mobile number. The controller for the main form looks like so:</p> <pre><code>//newsletter signup page public ActionResult NewsletterSignup() { var model = new NewsletterSignupModel(); return View(model); } [HttpPost, ActionName("NewsletterSignup")] public ActionResult NewsletterSignupSend(NewsletterSignupModel model) { if (ModelState.IsValid) { //register the user here } return View(model); } </code></pre> <p>On the main form I have a Validationsummary and validation for each of the fields. The problem is that my contoller states that the NewsletterSignupSend action can only be executed using HttpPost. Because the form on Page A uses HttpPost when the user arrives on Page B the validation has already been run - i.e. before the user has submitted the form on Page b.</p> <p>I know I'm missing a basic here - can someone steer me in the right direction?</p> <p>Thanks in advance Al</p> <p>UPDATE: To resolve this question I have done the following.</p> <p>Form A is rendered using:</p> <pre><code>@Html.Action("MiniNewsletterSignup") </code></pre> <p>Form A has a controller method:</p> <pre><code>//mini newsletter view public ActionResult MiniNewsletterSignup() { var model = new MiniNewsletterSignupModel(); return View(model); } </code></pre> <p>And the content of the view is:</p> <pre><code>@model Nop.Web.Models.Common.MiniNewsletterSignupModel @using (Html.BeginForm("NewsletterSignup", "Common")) { &lt;p&gt; &lt;input type="text" class="text" value="Enter your email address" id="email" name="email" /&gt; &lt;input type="submit" class="submit" value="Sign Up" /&gt; &lt;/p&gt; } </code></pre> <p>This submits the form using HttpPost off to Page B.</p> <p>Page B has 2 controller methods:</p> <pre><code>//newsletter signup page public ActionResult NewsletterSignup() { var model = new NewsletterSignupModel(); if (Request["email"] != null) model.Email = Request["email"]; return View(model); } </code></pre> <p>And:</p> <pre><code>[HttpPost, **WhenRequestContainsKey("FullName")**] public ActionResult NewsletterSignup(NewsletterSignupModel model) { if (ModelState.IsValid) { //process here } return View(model); } </code></pre> <p>You will notice I have added the Selector WhenRequestContainsKey which I found at <a href="http://softwaredevelopmentsolutions.blogspot.co.uk/2011/06/aspnet-mvc-3-partial-form-validation-on.html">http://softwaredevelopmentsolutions.blogspot.co.uk/2011/06/aspnet-mvc-3-partial-form-validation-on.html</a>. This means this code is only called when there is a field in the Request called FullName, which on our site only exists on Page B.</p> <p>This seems to work as I wanted it, but Im not sure why - why for example does this stop the validation taking place until the form is posted back on Page B - there is nothing in the method that calls a validate method???</p> <p>Is there anything wrong with the way Ive implemented this?</p> <p>Thanks Al</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload