Reputation: 683
This is my mvc validation code in model. It is working properly but just the problem is when some error in validation the form reset automatically. For example three of my fields are correct and one is empty when I click submit validation error will appear under the empty field but the field that I already provided is empty then.. How can I overcome this??
public class ContactModel
{
[Required (ErrorMessage="Name is Required")]
[StringLength(100)]
[Display(Order = 1)]
public string ContactName { get; set; }
[Required(ErrorMessage = "Email Address is Required")]
[RegularExpression(".+\\@.+\\..+", ErrorMessage = "Email Address is not Valid")]
[DataType(DataType.EmailAddress)]
[StringLength(254)]
[Display(Order = 2)]
public string Email { get; set; }
[Required(ErrorMessage = "Subject is Required")]
[Display(Order = 3)]
public string Subject { get; set; }
[Required(ErrorMessage = "Message is Required")]
[Display(Order = 3)]
public string Message { get; set; }
public bool MessageSent { get; set; }
//My View
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<label for="contactname">Name</label>
<input type="text" name="ContactName" class="textfield" id="contactname" value="" /><span class="require"> *</span>
@Html.ValidationMessageFor(model => model.ContactName)
<label for="contactsubject">Subject</label>
<input type="text" name="Subject" class="textfield" id="contactsubject" value=""/><span class="require"> *</span>
@Html.ValidationMessageFor(model => model.Subject)
<label for="contactemail">Your E-mail</label>
<input type="text" name="Email" class="textfield" id="contactemail" value="" /><span class="require"> *</span>
@Html.ValidationMessageFor(model => model.Email)
<label for="contactmessage">Your Message</label>
<textarea name="Message" id="contactmessage" class="textarea" cols="8" rows="12"></textarea><span class="require"> *</span>
@Html.ValidationMessageFor(model => model.Message)
<input name="Mysubmitted" id="Mysubmitted" value="Send Message" class="buttonsubmit" type="submit"/>
}
Upvotes: 0
Views: 99
Reputation: 19933
First of all change all inputs and labels to @Html.LabelFor(
and @Html.EditorFor
and if you want to set the class use @Html.TextBoxFor
in your view and recheck it
Upvotes: 1