I am using MVC and Razor for the first time and, in our opinion, have a very simple question. I have two pages on the site. The first page (page A) has a very small form on which there is an input for the email address. when a user enters their email address and sends clicks, they are sent to page B using HttpPost, for example:
@using (Html.BeginForm("NewsletterSignup","Common", FormMethod.Post)) { <p> <input type="text" class="text" value="Enter your email address" id="email" name="email" /> <input type="submit" class="submit" value="Sign Up" /> </p> }
Page B has a basic form that allows the user to also enter their name and mobile number. The controller for the main form looks like this:
//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); }
In the main form, I have Validationsummary and validation for each of the fields. The problem is that my contoller claims that the NewsletterSignupSend action can only be executed using HttpPost. Since the form on page A uses HttpPost when the user comes to page B, the verification has already been completed - that is, before the user submits the form on page b.
I know that I do not have basic information - can someone direct me in the right direction?
Thanks in advance Al
UPDATE. To solve this issue, I did the following.
Form A is displayed using:
@Html.Action("MiniNewsletterSignup")
Form A has a controller method:
//mini newsletter view public ActionResult MiniNewsletterSignup() { var model = new MiniNewsletterSignupModel(); return View(model); }
And the content of the presentation:
@model Nop.Web.Models.Common.MiniNewsletterSignupModel @using (Html.BeginForm("NewsletterSignup", "Common")) { <p> <input type="text" class="text" value="Enter your email address" id="email" name="email" /> <input type="submit" class="submit" value="Sign Up" /> </p> }
This submits the form using HttpPost to page B.
Page B has two control methods:
//newsletter signup page public ActionResult NewsletterSignup() { var model = new NewsletterSignupModel(); if (Request["email"] != null) model.Email = Request["email"]; return View(model); }
and
[HttpPost, **WhenRequestContainsKey("FullName")**] public ActionResult NewsletterSignup(NewsletterSignupModel model) { if (ModelState.IsValid) {
You will notice that I added the Selector WhenRequestContainsKey, which I found at http://softwaredevelopmentsolutions.blogspot.co.uk/2011/06/aspnet-mvc-3-partial-form-validation-on.html . This means that this code is only called if there is a field in the request with the name FullName, which on our site exists only on page B.
This seems to work the way I wanted it to, but I'm not sure why - why, for example, it stops the validation until the form is submitted to page B - there is nothing in the method that invokes the validation method ???
Is there something wrong with how Ive implemented this?
Thanks al