Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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");
        }
    }
Posted
Updated 15-Dec-14 20:00pm
v2
Comments
Rahul Rajat Singh 9-Dec-14 1:08am    
Could you show us your view code. Also, perhaps the validations are getting pulled from DataAnnotations put on the model. Check the model property associated with the dropdown.

1 solution

Let me Google[^] that for you...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900