ModelState always invalid despite correct information in form fields
HI all,
I am having some serious issues with Surface Controllers and getting my model to validate despite all of the information being in the structure I am expecting it to be.
Here is the code for my model:
public class EnquiryModel
{
[UmbracoRequired("Enquiry.Error.Required")]
[UmbracoDisplayName("Enquiry.Name")]
public string Name { get; set; }
[UmbracoEmail(ErrorMessageDictionaryKey = "Enquiry.Error.Email")]
[UmbracoRequired("Enquiry.Error.Required")]
[UmbracoDisplayName("Enquiry.Email")]
public string Email { get; set; }
[UmbracoDisplayName("Enquiry.Telephone")]
public int Telephone { get; set; }
[UmbracoRequired("Enquiry.Error.Required")]
[UmbracoDisplayName("Enquiry.Message")]
public string Message { get; set; }
}
And my controller:
public class EnquiryController : SurfaceController
{
[HttpPost]
public ActionResult Submit(EnquiryModel model)
{
string nameValue = model.Name;
string emailValue = model.Email;
int telephoneValue = model.Telephone;
string messageValue = model.Message;
if (!ModelState.IsValid)
return CurrentUmbracoPage();
// Create a regular expression to remove script tags
Regex regex = new Regex(@"<script(.+?)*</script>");
string request = regex.Replace(model.Message, string.Empty);
request = request + "<br/><br/>" + "Phone: " + model.Telephone + "<br/><br/>" + "Email: " + model.Email;
MailMessage message = new MailMessage();
message.From = new MailAddress(model.Email);
message.To.Add(new MailAddress("[email protected]"));
message.Subject = "New Website Enquiry - " + model.Name;
message.Body = request;
SmtpClient client = new SmtpClient();
try
{
client.Send(message);
TempData["success"] = true;
}
catch (Exception ex)
{
TempData["error"] = true;
return CurrentUmbracoPage();
}
return RedirectToCurrentUmbracoPage();
}
}
I have tested all of the values needed by the model and they are all correct yet the code always bugs out at the model validity checker. What is wrong here?
ModelState always invalid despite correct information in form fields
HI all,
I am having some serious issues with Surface Controllers and getting my model to validate despite all of the information being in the structure I am expecting it to be.
Here is the code for my model:
And my controller:
And finally my view:
I have tested all of the values needed by the model and they are all correct yet the code always bugs out at the model validity checker. What is wrong here?
So all the values in the EnquiryModel are correctly assigned when posted to the server? Did you set a breakpoint before
.IsValid
and check this?is working on a reply...