如何在MVC4中验证表单? [英] How do I validate a form in MVC4?

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

问题描述

大家好,



我使用MVC4。保存数据时我的项目有问题。

我的视图中有少量下拉,文本框,日期选择器。我已经为下拉列表编写了验证检查。但是当我按下提交按钮时,表单没有发布,但是它保持在没有验证检查的下拉状态(下拉状态)之一。

但是当我输入所有字段时在表格中,表格已张贴。



任何人请帮助我。如果您需要更多数据,请告诉我..



我的模特

Hi all,

I working with MVC4. I have a problem in my project while saving data.
I have few drop downs, text boxes, date pickers in my view. I have written validation checks for a drop down. But when I press the submit button, the form is not posting, but it stays at one of the drop down (drop down for state) that has no validation check.
But when I enter all the fields in the form, the form is posted.

any one please help me. Please let me know if you need any more data..

My Model

public class ContactUsForm
    {
        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 enter name")]
        public string unCandidateFirstName { get { return _unCandidateFirstName; } set { _unCandidateFirstName = value; } }

        private string _unCandMobilePhone;
        [Required(ErrorMessage = "Please enter mobile number")]
        [RegularExpression(@"^[0-9]*$", ErrorMessage = "Please enter valid phone number")]
        [StringLength(50,ErrorMessage="Phone number must be between 10-50 digits",MinimumLength=10)]
        public string unCandMobilePhone { get { return _unCandMobilePhone; } set { _unCandMobilePhone = value; } }

        private string _unCandEmail;
        [Required(ErrorMessage = "Please enter email")]
        [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Please enter a valid e-mail adress")]
        public string unCandEmail { get { return _unCandEmail; } set { _unCandEmail = value; } }
        private int _countryID;
        [Required(ErrorMessage = "Please select country")]
        public int countryID { get { return _countryID; } set { _countryID = value; } }
        public SelectList countries { get { return LoadCountries(); } }

        private int _stateID;
        public int stateID { get { return _stateID; } set { _stateID = 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");
        }
    }

推荐答案

,ErrorMessage =请输入有效的电话号码)]
[StringLength(50,ErrorMessage =电话号码必须介于10-50位之间,MinimumLength = 10)]
公共字符串unCandMobilePhone {get {return _unCandMobilePhone;} set {_unCandMobilePhone = value;}}

私有字符串_unCandEmail;
[必填(ErrorMessage =请输入电子邮件)]
[RegularExpression(@^([a-zA-Z0-9_\-\。 ] +)@((\ [[0-9] {1,3} \ [0-9] {1,3} \ [0-9] {1,3} \。)| ((A-ZA-Z0-9\ - ] + \)+。))([A-ZA-Z] {2,4} | [0-9] {1,3})(\ ]?)
", ErrorMessage = "Please enter valid phone number")] [StringLength(50,ErrorMessage="Phone number must be between 10-50 digits",MinimumLength=10)] public string unCandMobilePhone { get { return _unCandMobilePhone; } set { _unCandMobilePhone = value; } } private string _unCandEmail; [Required(ErrorMessage = "Please enter email")] [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)


,ErrorMessage =请输入有效的电子邮件地址)]
公共字符串unCandEmail {get {return _unCandEmail;} set {_unCandEmail = value;
private int _countryID;
[必需(ErrorMessage =请选择国家)]
public int countryID {get { return _countryID;} set {_countryID = value;
public SelectList countries {get {return LoadCountries();

private int _stateID;
public int stateID {get {return _stateID; } set {_stateID = 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();
返回新的SelectList(countryList,countryID,country);
}
}
", ErrorMessage = "Please enter a valid e-mail adress")] public string unCandEmail { get { return _unCandEmail; } set { _unCandEmail = value; } } private int _countryID; [Required(ErrorMessage = "Please select country")] public int countryID { get { return _countryID; } set { _countryID = value; } } public SelectList countries { get { return LoadCountries(); } } private int _stateID; public int stateID { get { return _stateID; } set { _stateID = 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"); } }


让我 Google [ ^ ]为你...
Let me Google[^] that for you...


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

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