如何在MVC 4中验证我的表单 [英] How do I validate my form in MVC 4

查看:73
本文介绍了如何在MVC 4中验证我的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用MVC4。我的表格有问题。我在我的模型中提供了验证检查。但即使在将数据填充到具有验证检查的字段之后,控制器中的相应操作也不会在提交表单时触发。但是当所有字段都被填满时触发。



  public  ContactUsForm( )
{
statesList = new SelectList( new List< SelectListItem>() , stateID 状态);
provinceList = new SelectList( new List< SelectListItem>(), provinceID );
}
public SelectList statesList { get ; set ; }

public SelectList provinceList { get ; set ; }

private string _unCandidateFirstName;
[必需(ErrorMessage = 请输入您的姓名)]
< span class =code-keyword> public
string unCandidateFirstName { get { return _unCandidateFirstName; } set {_ unCandidateFirstName = value ; }

private string _unCandMobilePhone;
[必填(ErrorMessage = 请输入您的手机号码)]
public string unCandMobilePhone { get { return _unCandMobilePhone; } set {_ unCandMobilePhone = value ; }

private string _unCandEmail;
[必需(ErrorMessage = 请输入您的电子邮件)]
< span class =code-keyword> public string unCandEmail { get { return _unCandEmail; } set {_ unCandEmail = value ; }

private string _unCandLandLine;
public string unCandLandLine { get { return _unCandLandLine; } set {_unCandLandLine = value ;

private int _countryID;
[显示(名称= 国家)]
public int countryID { get { return _countryID; } set {_countryID = value ; }
public SelectList国家/地区{获取 { return LoadCountries();

private int _stateID;
[显示(名称= 状态)]
public int stateID { get { return _stateID; } set {_stateID = value ; }

private int _provinceID;
public int provinceID { get { return _provinceID; } set {_provinceID = value ; }

private string _unCandEnquiry;
public string unCandEnquiry { get { return _unCandEnquiry; } set {_unCandEnquiry = value ; }

私人 Guid _userID;
public Guid userID { get { return _userID; } set {_ userID = value ; }

private int _referenceId;
public int referenceId { get { return _referenceId; } set {_ referenceId = value ;

private SelectList LoadCountries()
{
SM_WEB.Models.SMWebDefaultConnection objSMWebDefaultConnection = new SM_WEB.Models.SMWebDefaultConnection();
var countryList = objSMWebDefaultConnection.smConn.tblCountries.Where(c = > c.recordActive == true )。选择(c = > new {c.countryID,c.country})。ToList();
return new SelectList(countryList, countryID country);
}





任何人都可以帮助我..

解决方案

< blockquote>模型验证可以在Page post back期间完成。您需要使用jquery


进行客户端验证

Hi All,
I am working with MVC4. I have a problem in my form. I have provided validation checks in my model. But even after filling data to the fields having validation checks, the corresponding action in the controller is not triggering when form is submitted. But triggers when all the fields are filled.

public ContactUsForm()
        {
            statesList = new SelectList(new List<SelectListItem>(), "stateID", "state");
            provinceList = new SelectList(new List<SelectListItem>(), "provinceID", "province");
        }
        public SelectList statesList { get; set; }

        public SelectList provinceList { get; set; }

        private string _unCandidateFirstName;
        [Required(ErrorMessage = "Please type your name")]
        public string unCandidateFirstName { get { return _unCandidateFirstName; } set { _unCandidateFirstName = value; } }

        private string _unCandMobilePhone;
        [Required(ErrorMessage = "Please type your mobile number")]
        public string unCandMobilePhone { get { return _unCandMobilePhone; } set { _unCandMobilePhone = value; } }

        private string _unCandEmail;
        [Required(ErrorMessage = "Please type your email")]
        public string unCandEmail { get { return _unCandEmail; } set { _unCandEmail = value; } }

        private string _unCandLandLine;
        public string unCandLandLine { get { return _unCandLandLine; } set { _unCandLandLine = value; } }

        private int _countryID;
        [Display(Name = "Country")]
        public int countryID { get { return _countryID; } set { _countryID = value; } }
        public SelectList countries { get { return LoadCountries(); } }

        private int _stateID;
        [Display(Name = "State")]
        public int stateID { get { return _stateID; } set { _stateID = value; } }

        private int _provinceID;
        public int provinceID { get { return _provinceID; } set { _provinceID = value; } }

        private string _unCandEnquiry;
        public string unCandEnquiry { get { return _unCandEnquiry; } set { _unCandEnquiry = value; } }

        private Guid _userID;
        public Guid userID { get { return _userID; } set { _userID = value; } }

        private int _referenceId;
        public int referenceId { get { return _referenceId; } set { _referenceId = value; } }

        private SelectList LoadCountries()
        {
            SM_WEB.Models.SMWebDefaultConnection objSMWebDefaultConnection = new SM_WEB.Models.SMWebDefaultConnection();
            var countryList = objSMWebDefaultConnection.smConn.tblCountries.Where(c => c.recordActive == true).Select(c => new { c.countryID, c.country }).ToList();
            return new SelectList(countryList, "countryID", "country");
        }



Can any body help me..

解决方案

The Model validation can be done during the Page post back. you need to do the client side validation by using jquery


这篇关于如何在MVC 4中验证我的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆