Reputation: 2642
This is my model of my asp.net mvc 2 project :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.DynamicData;
using System.Web.Security;
using System.Globalization;
using System.Web.Profile;
namespace EMS.Models
{
public class UserModels
{
[Required(ErrorMessage = "*")]
public string userName { get; set; }
[Required(ErrorMessage = "*")]
public string passWord { get; set; }
}
}
And this is my view :
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm()) { %>
<table>
<tr>
<td><label for="userlogin">User Login:</label></td>
<td>
<%: Html.TextBoxFor(m => m.userName, new { id = "userName"})%>
<%: Html.ValidationMessageFor(m => m.name)%>
</td>
<td><label for="password">Password:</label></td>
<td>
<%: Html.PasswordFor(m => m.passWord, new { id = "password"})%>
<%: Html.ValidationMessageFor(m => m.passWord)%>
</td>
</tr>
<tr>
<td colspan="4">
<input type="submit" name="enter_infor" id="enter_infor" value="Enter Information"/>
</td>
</tr>
</table>
And this is my controller :
[HttpPost]
public ActionResult UserMaintenance(FormCollection frm)
{
UserModels candidate = new UserModels
{
userName = frm["userName"].ToString(),
passWord = frm["passWord"].ToString()
};
DBSEntities context = new DBSEntities();
UserName user = new UserName();
context.AddToUserNames(user);
context.SaveChanges();
return View();
}
Problem : I want to validate, whether the user input the both, user name and password textbox or not. But all of my above code, the user still can submit it without any validate message. I included the scripts to Site.Master ready. Anyone can tell me, what did I wrong?
Thanks.
Upvotes: 1
Views: 733
Reputation: 32768
You should use ModelState.IsValid
in controller action else all the validations that you setup are useless.
public ActionResult UserMaintenance(UserName user)
{
if(ModelState.IsValid) // save the user only if it is valid
{
DBSEntities context = new DBSEntities();
context.AddToUserNames(user);
context.SaveChanges();
return RedirectToAction("Index") // redirect to some other action or whatevery you wish
}
return View(); // else return the same view this will show all the validation errors
}
Upvotes: 2
Reputation: 39827
One thing is that you are not checking the model state during your POST and really should be using strongly typed Controller/Actions and Views. Even once you get ClientValidation working, you should still do a server side check to ensure that the client stuff was not bypassed. You could change your POST method to the following
[HttpPost]
public ActionResult UserMaintenance(UserModels candidate)
{
//If the model is not valid, return the view with the posted values
//Validation messages will appear where appropriate based on your
//View Model
if(!modelState.isValid()){
return View(candidate);
}
DBSEntities context = new DBSEntities();
//Not sure what this code was doing, you were creating a variable
//and then never setting the properties from the FormCollection on
//this new variable. This seems to be the correct way below, but
//whatever you need to do to get this POSTed info to your DB....
context.AddToUserNames(candidate);
context.SaveChanges();
return View();
}
As far as the ClientSideValidation stuff, double check that you have included the proper scripts and in the proper order (can you list those out in your question?)
Upvotes: 2