Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Nov 27, 2012 10:55 AM by CPrakash82
Member
331 Points
456 Posts
Nov 26, 2012 03:10 PM|LINK
Morning/Afternoon guys and girls,
I have created a MVC web application i have a slight problem with the validation on the contact us form page, when JS is enabled the validation works,
When i disable JS the validation no longers works.. can someone please tell me what im missing.
This is my model
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace First_For_Aid { public class Contact { [Required(ErrorMessage = " * Required")] [DisplayName("Name")] public string Name { get; set; } [Required(ErrorMessage = " * Required")] [DataType(DataType.EmailAddress, ErrorMessage = "Your email address contains some errors")] [DisplayName("Email address")] public string Email { get; set; } [Required(ErrorMessage = " * Required")] [DisplayName("Subject")] public string Subject { get; set; } [Required(ErrorMessage = " * Required")] [DisplayName("Message")] public string Message { get; set; } } }
and this is my Contact us page
<%@ Page Title="Contact Us" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<First_For_Aid.Contact>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <div class="body_big"> <h2>Contact Us<br /><span>Please use the below form to contact First For Aid</span></h2> <div class="bg"></div> <div id="contactform"> <% Html.EnableClientValidation(); %> <% using (Html.BeginForm()) { %> <%: Html.ValidationSummary(true, "A few fields are still empty") %> <div class="editor-label"><%: Html.LabelFor(m => m.Name) %><%: Html.TextBoxFor(m => m.Name) %><%: Html.ValidationMessageFor(m => m.Name) %></div> <div class="editor-label"><%: Html.LabelFor(m => m.Email) %><%: Html.TextBoxFor(m => m.Email) %><%: Html.ValidationMessageFor(m => m.Email) %></div> <div class="editor-label"><%: Html.LabelFor(m => m.Subject) %><%: Html.TextBoxFor(m => m.Subject) %><%: Html.ValidationMessageFor(m => m.Subject) %></div> <div class="editor-label"><%: Html.LabelFor(m => m.Message)%><%: Html.TextAreaFor(m => m.Message, 5, 30, null)%><%: Html.ValidationMessageFor(m => m.Message)%></div> <p> <button type="submit" value="submit" class="contactformButton">Submit</button> </p> <% } %> </div> </div>Can some one please help me
All-Star
18286 Points
2842 Posts
Nov 26, 2012 05:01 PM|LINK
Harrison.Scott When i disable JS the validation no longers works.. can someone please tell me what im missing.
It should fire up the server validation, do you have ModelState.IsValid validation check performed in your code.
Nov 27, 2012 07:42 AM|LINK
Hi thanks for the reply, im new to MVC so this is what i have in my home controller
[AcceptVerbs(HttpVerbs.Get)] public ActionResult ContactUs() { Contact objContact = new Contact(); return View(objContact); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult ContactUs(Contact objContact) { SendEmail objSendEmail = new SendEmail(); objSendEmail.SendFirstForAidEmail(objContact); return RedirectToAction("thank-you", "Home"); }
Nov 27, 2012 10:55 AM|LINK
Change it like this and follow the validation tutorial here
[AcceptVerbs(HttpVerbs.Post)] public ActionResult ContactUs(Contact objContact) { if(ModelState.IsValid) { SendEmail objSendEmail = new SendEmail(); objSendEmail.SendFirstForAidEmail(objContact); return RedirectToAction("thank-you", "Home"); } return View(objContact); }
Harrison.Sco...
Member
331 Points
456 Posts
Contact us form, not validating on server side what JS is disabled.
Nov 26, 2012 03:10 PM|LINK
Morning/Afternoon guys and girls,
I have created a MVC web application i have a slight problem with the validation on the contact us form page, when JS is enabled the validation works,
When i disable JS the validation no longers works.. can someone please tell me what im missing.
This is my model
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace First_For_Aid { public class Contact { [Required(ErrorMessage = " * Required")] [DisplayName("Name")] public string Name { get; set; } [Required(ErrorMessage = " * Required")] [DataType(DataType.EmailAddress, ErrorMessage = "Your email address contains some errors")] [DisplayName("Email address")] public string Email { get; set; } [Required(ErrorMessage = " * Required")] [DisplayName("Subject")] public string Subject { get; set; } [Required(ErrorMessage = " * Required")] [DisplayName("Message")] public string Message { get; set; } } }and this is my Contact us page
<%@ Page Title="Contact Us" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<First_For_Aid.Contact>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <div class="body_big"> <h2>Contact Us<br /><span>Please use the below form to contact First For Aid</span></h2> <div class="bg"></div> <div id="contactform"> <% Html.EnableClientValidation(); %> <% using (Html.BeginForm()) { %> <%: Html.ValidationSummary(true, "A few fields are still empty") %> <div class="editor-label"><%: Html.LabelFor(m => m.Name) %><%: Html.TextBoxFor(m => m.Name) %><%: Html.ValidationMessageFor(m => m.Name) %></div> <div class="editor-label"><%: Html.LabelFor(m => m.Email) %><%: Html.TextBoxFor(m => m.Email) %><%: Html.ValidationMessageFor(m => m.Email) %></div> <div class="editor-label"><%: Html.LabelFor(m => m.Subject) %><%: Html.TextBoxFor(m => m.Subject) %><%: Html.ValidationMessageFor(m => m.Subject) %></div> <div class="editor-label"><%: Html.LabelFor(m => m.Message)%><%: Html.TextAreaFor(m => m.Message, 5, 30, null)%><%: Html.ValidationMessageFor(m => m.Message)%></div> <p> <button type="submit" value="submit" class="contactformButton">Submit</button> </p> <% } %> </div> </div>
Can some one please help me
CPrakash82
All-Star
18286 Points
2842 Posts
Re: Contact us form, not validating on server side what JS is disabled.
Nov 26, 2012 05:01 PM|LINK
It should fire up the server validation, do you have ModelState.IsValid validation check performed in your code.
Harrison.Sco...
Member
331 Points
456 Posts
Re: Contact us form, not validating on server side what JS is disabled.
Nov 27, 2012 07:42 AM|LINK
Hi thanks for the reply, im new to MVC so this is what i have in my home controller
[AcceptVerbs(HttpVerbs.Get)] public ActionResult ContactUs() { Contact objContact = new Contact(); return View(objContact); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult ContactUs(Contact objContact) { SendEmail objSendEmail = new SendEmail(); objSendEmail.SendFirstForAidEmail(objContact); return RedirectToAction("thank-you", "Home"); }CPrakash82
All-Star
18286 Points
2842 Posts
Re: Contact us form, not validating on server side what JS is disabled.
Nov 27, 2012 10:55 AM|LINK
Change it like this and follow the validation tutorial here
[AcceptVerbs(HttpVerbs.Post)] public ActionResult ContactUs(Contact objContact) { if(ModelState.IsValid) { SendEmail objSendEmail = new SendEmail(); objSendEmail.SendFirstForAidEmail(objContact); return RedirectToAction("thank-you", "Home"); } return View(objContact); }