I have a couple things going on right now. I have a form that I am trying to do client side validation on, and it is working however, if I click submit with one of the fields not being valid, it will still send the information to the client which I don’t want to do until all of the information is correct.
The other problem I am having is I keep getting a “http-status code 500” when I click the submit button, and the form will still work and send out email even though I am getting this 500 error(using fiddler2 it shows this, otherwise I wouldn’t know). Can anyone please tell me why this is, and how I can remedy it please. Thank you guys.
Here is my form:
<div id="Contact">
<% Html.EnableClientValidation(); %>
<%using (Html.BeginForm("SendContactMessage", "ContactUs", FormMethod.Post))
{%>
<table cellpadding="0" cellspacing="0" border="0" width="900" height="100%">
<tr>
<td id="body" class="Content">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="right" valign="middle">
<label for="name" id="name">First & Last Name</label><br>
<label class="req">
<%=Html.ValidationMessageFor(a => a.ContactName)%>
Required</label>
</td>
<td class="fieldareacontact">
<%= Html.TextBox("ContactName", Model.ContactName, new { id = "Name", title = "name", @class = "fieldscontact" })%>
</td>
</tr>
<tr>
<td align="right" valign="middle">
<label>Email Address</label><br>
<label class="req">
<%=Html.ValidationMessageFor(a => a.ContactEmail)%>
Required
</label>
</td>
<td class="fieldareacontact">
<%= Html.TextBox("ContactEmail", Model.ContactEmail, new { id = "Email", title = "email", @class = "fieldscontact" })%>
</td>
</tr>
<tr>
<td align="right" valign="top" style="padding-top: 15px">
<label>Whatcha' Got?</label>
</td>
<td class="fieldareacontact">
<%= Html.TextArea("ContactCommentsOrQuestions", Model.ContactCommentsOrQuestions, new { Style = "width: 260px; height:150px", id = "CommentsOrQuestions", title = "comments", @class = "fieldscontact" })%>
</td>
<%= Html.ValidationMessage("Contacterror")%>
<% if (!ViewData.ModelState.IsValid)
{%><br /><%} %>
</tr>
<tr>
<td align="right" valign="top" style="padding-top: 15px">
</td>
<td align="right" valign="top" style="padding-top: 15px">
<input type="submit" id="submit" value="Submit" class="button" />
</td>
</tr>
</table>
<%} %>
</div>
Here are a couple of my controllers:
public ActionResult Index()
{
ContactUsViewModel model = new ContactUsViewModel();
return View(model);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SendContactMessage(ContactUsViewModel model)
{
if (this.ModelState.IsValid)
{
try
{
// get contact configuration
DoSendMessage(model);
//return View(model);
return View(new ContactUsViewModel());
}
catch (Exception ex)
{
//TODO: log/notify error
//while( ex != null ){
//Response.Write( "<HR>" + ex.ToString() );
//ex = ex.InnerException;
//}
ModelState.AddModelError("Contacterror", "An unexpected error occured, please contact the webmaster.");
return View();
}
}
else
{
ModelState.AddModelError("Contacterror", "Please correct the above with a \"*\".");
return View(model);
}
}
The DoSendMessage() just sends the email notifications, and I know that function works, since even with the 500 error, I still can somehow get emails.
Here is the 500 error:
Server Error in ‘/’ Application.
The view ‘SendContactMessage’ or its master was not found. The following locations were searched:
~/Views/ContactUs/SendContactMessage.aspx
~/Views/ContactUs/SendContactMessage.ascx
~/Views/Shared/SendContactMessage.aspx
~/Views/Shared/SendContactMessage.ascx
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The view ‘SendContactMessage’ or its master was not found. The following locations were searched:
~/Views/ContactUs/SendContactMessage.aspx
~/Views/ContactUs/SendContactMessage.ascx
~/Views/Shared/SendContactMessage.aspx
~/Views/Shared/SendContactMessage.ascx
Why is it looking for a page, when I’m calling a method? Please help, and thank you all in advance, I’m sorry if this is a novice question, but I wasn’t able to find more resources on why I am having these errors.
Here is the DataAnnotation:
public class ContactUsViewModel
{
[Required(ErrorMessage = "*")]
public string ContactName { get; set; }
[Required(ErrorMessage = "*")]
//this is my own regular expression that I created
[RegularExpression("^[a-zA-Z0-9]+[\\.a-zA-Z0-9_]*[@][.a-zA-Z0-9]+[.]([a-z]{2,4})$", ErrorMessage = "*")]
public string ContactEmail { get; set; }
//[Required(ErrorMessage = "*")]
public string ContactCommentsOrQuestions { get; set; }
}
Modify your
elsestatement: