Wrox Programmer Forums
|
BOOK: Professional ASP.NET MVC 4
This is the forum to discuss the Wrox book Professional ASP.NET MVC 4 by Jon Galloway, Phil Haack, Brad Wilson, K. Scott Allen; ISBN: 978-1-118-34846-8
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET MVC 4 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 11th, 2013, 11:39 AM
Registered User
 
Join Date: Sep 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Proper Validations

My validation message is not displaying... i typed [Required] before the properties in one of my models and then in the view i wrote
Code:
@html.ValidationSummary(true)
and also wrote
Code:
@html.validationMessageFor(m=>m.property)
for each .. but message is not displaying ....

this is my code..
Code:
@model MyMVCApp.Models.Surveyor
@{
ViewBag.Title = "Log in";
Layout = "~/Views/Shared/_LayoutPage1.cshtml";
}
<h1>@ViewBag.Title</h1>
@using (Html.BeginForm()) {

@Html.ValidationSummary(true)

<fieldset>
<legend>Log in Form</legend>

@Html.LabelFor(m => m.SurveyorName)
@Html.TextBoxFor(m => m.SurveyorName)
@Html.ValidationMessageFor(m => m.SurveyorName)

@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)

<input type="submit" value="Log in"/>
</fieldset>
<br />
}
 
Old September 11th, 2013, 02:30 PM
Authorized User
 
Join Date: Jun 2013
Posts: 36
Thanks: 1
Thanked 4 Times in 4 Posts
Default

What's in the controller?
Did you check if Model.IsValid?
 
Old September 12th, 2013, 12:55 PM
Registered User
 
Join Date: Sep 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default controller's code

Code:
  
        public ActionResult Login() 
        {
            //ViewResult v = new ViewResult();
            //v.ViewName = "~/Views/Account/Login.cshtml";
            return View();
        }

        [HttpPost]
        public ActionResult Login(Surveyor s)
        { 
            return RedirectToAction("Registered"); 
        }
 
Old September 12th, 2013, 04:01 PM
Authorized User
 
Join Date: Jun 2013
Posts: 36
Thanks: 1
Thanked 4 Times in 4 Posts
Default

1. When the user submits the form (i.e. does a POST request), your [HttpPost] Login action will be called.
2. Before that ModelBinder creates an instance of "Surveyor"-class and tries to extract values from your form and put them into properties of that class
3. And then... nothing.
The request is automatically redirected to another action - to the "Registered"-action - regardless of what was there in the submitted form.

You need to do something. What exactly - I don't know. May be look up this object in the database and if it exists there - then allow to proceed.

Anyway, this code is not from the book, so I cannot tell you exactly what to do next.
 
Old September 13th, 2013, 01:02 PM
Registered User
 
Join Date: Sep 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default validation

i asked u that y my validation message is not displaying??
 
Old September 13th, 2013, 01:37 PM
Authorized User
 
Join Date: Jun 2013
Posts: 36
Thanks: 1
Thanked 4 Times in 4 Posts
Default

Of course - there is no reason for the validation messages to appear because you are not validating your model.

You need something like:

if (Model.IsValid)
{ // Save your data }
else
{ return View(); }
 
Old September 14th, 2013, 01:47 PM
Registered User
 
Join Date: Sep 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default not working

here is my code
Code:
if (ModelState.IsValid)
            {
                SurveyContext sc = new SurveyContext();
                sc.Surveyor.Add(s);
                sc.SaveChanges();
            }
            return RedirectToAction("Registered");
it's going to "Registered" action instead showing validation messages





Similar Threads
Thread Thread Starter Forum Replies Last Post
Validations in dataGrid p2pMember ASP.NET 1.0 and 1.1 Basics 1 December 7th, 2005 08:17 PM
problem with validations ashokparchuri General .NET 1 April 14th, 2005 08:20 AM
Validations... Flunders Beginning VB 6 3 September 29th, 2004 07:09 AM
Validations through ASP ashu_gupta75 Classic ASP Basics 7 April 21st, 2004 06:07 AM
Validations not working Ajay Jain ASP.NET 1.0 and 1.1 Basics 1 February 24th, 2004 01:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.