本文介绍了如何在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");
        }
    }

推荐答案




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

08-20 08:14
查看更多