Html.ValidationMessageFor在asp.net mvc 2中不起作用

时间:2012-06-25 12:48:26

标签: asp.net-mvc

这是我的asp.net mvc 2项目的模型:

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; }
  }
}

这是我的观点:

 <% 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">
                    &nbsp; <input type="submit"  name="enter_infor" id="enter_infor" value="Enter Information"/>
                </td>
            </tr>
    </table>

这是我的控制者:

    [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();
    }

问题:我想验证用户是否同时输入用户名和密码文本框。但是我上面的所有代码,用户仍然可以在没有任何验证消息的情况下提交它。我将脚本包含到Site.Master中。任何人都可以告诉我,我错了什么?

感谢。

2 个答案:

答案 0 :(得分:2)

有一点是你在POST期间没有检查模型状态,而且应该使用强类型控制器/动作和视图。即使您使ClientValidation正常工作,您仍应进行服务器端检查以确保未绕过客户端内容。您可以将POST方法更改为以下

 [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();
    }

就ClientSideValidation而言,请仔细检查您是否已按正确的顺序包含正确的脚本(可以在问题中列出这些脚本吗?)

答案 1 :(得分:2)

您应该在控制器操作中使用ModelState.IsValid,否则您设置的所有验证都是无用的。

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
}