Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 11th, 2013, 11:39 AM
Registered User
Points: 26, Level: 1
Points: 26, Level: 1 Points: 26, Level: 1 Points: 26, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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 />
}
  #2 (permalink)  
Old September 11th, 2013, 02:30 PM
Authorized User
Points: 175, Level: 3
Points: 175, Level: 3 Points: 175, Level: 3 Points: 175, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2013
Location: St.Petersburg, Russia
Posts: 36
Thanks: 1
Thanked 4 Times in 4 Posts
Default

What's in the controller?
Did you check if Model.IsValid?
  #3 (permalink)  
Old September 12th, 2013, 12:55 PM
Registered User
Points: 26, Level: 1
Points: 26, Level: 1 Points: 26, Level: 1 Points: 26, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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"); 
        }
  #4 (permalink)  
Old September 12th, 2013, 04:01 PM
Authorized User
Points: 175, Level: 3
Points: 175, Level: 3 Points: 175, Level: 3 Points: 175, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2013
Location: St.Petersburg, Russia
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.
  #5 (permalink)  
Old September 13th, 2013, 01:02 PM
Registered User
Points: 26, Level: 1
Points: 26, Level: 1 Points: 26, Level: 1 Points: 26, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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??
  #6 (permalink)  
Old September 13th, 2013, 01:37 PM
Authorized User
Points: 175, Level: 3
Points: 175, Level: 3 Points: 175, Level: 3 Points: 175, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2013
Location: St.Petersburg, Russia
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(); }
  #7 (permalink)  
Old September 14th, 2013, 01:47 PM
Registered User
Points: 26, Level: 1
Points: 26, Level: 1 Points: 26, Level: 1 Points: 26, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 05:36 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.