Wyatt Wyatt - 3 months ago 31
ASP.NET (C#) Question

ASP.NET MVC 4 ModelState.IsValid is always false

I realize that this question has been asked before, but I do not see a solution to my issue anywhere. So, I am trying to familiarize myself with MVC. I am using MVC 4, with bootstrap. I am trying to create a contact form. I was following a tutorial and got this below.

Here is the viewmodel data:

public class ContactViewModel
{
[Required]
[StringLength(20, MinimumLength = 5)]
public string Name { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
public string Subject { get; set; }
[Required]
public string Message { get; set; }

}


Here is the cshtml data:

<div class="container">
<div class="row">
@if (ViewBag.Message == null)
{
<div>
<form method="post">
<div class="form-group">
<label asp-for="Name">Name</label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name"
class="text-muted"></span>
</div>
<div class="form-group">
<label asp-for="Email">Email</label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email"
class="text-muted"></span>
</div>
<div class="form-group">
<label asp-for="Subject">Subject</label>
<input asp-for="Subject" class="form-control" />
<span asp-validation-for="Subject"
class="text-muted"></span>
</div>
<div class="form-group">
<label asp-for="Message">Message</label>
<textarea rows="5" cols="15"
asp-for="Message" class="form-control"></textarea>
<span asp-validation-for="Message"
class="text-muted"></span>
</div>
<div>
<button type="submit" class="btn">
Send
</button>
</div>

</form>
</div>
}

<div>
<div>
@if (ViewBag.Message != null){
<div>@ViewBag.Message</div>
}
</div>
</div>
</div>
</div>


Here is the controller data:

[HttpGet]
public ActionResult Contact()
{
ViewBag.Message = null;

return View();
}

[HttpPost]
public ActionResult Contact(ContactViewModel vm)
{
if (ModelState.IsValid)
{
try
{
MailMessage msz = new MailMessage();
msz.From = new MailAddress(vm.Email);//Email which you are getting
//from contact us page
msz.To.Add("email");//Where mail will be sent
msz.Subject = vm.Subject;
msz.Body = vm.Message;
SmtpClient smtp = new SmtpClient();

smtp.Host = "smtp.gmail.com";

smtp.Port = 587;

smtp.Credentials = new System.Net.NetworkCredential
("user", "password has been removed");

smtp.EnableSsl = true;

smtp.Send(msz);

ModelState.Clear();
ViewBag.Message = "Thanks, your message has been sent.";
}
catch (Exception ex)
{
ModelState.Clear();
ViewBag.Message = $"It seems an error has been encountered. Please, try again later. Thank you. If you contact customer support, send them this : {ex.Message}";
}
} else
{
var errors = ModelState.SelectMany(x => x.Value.Errors.Select(z => z.Exception));
}

return View();
}


The output of the var 'errors' =

{System.Linq.Enumerable.<SelectManyIterator>d__16<System.Collections.Generic.KeyValuePair<string, System.Web.Mvc.ModelState>, System.Exception>}


My issue is that the code always says that ModelState.IsValid is always false. Why is that so? Thank you.

EDIT:
I forgot to mention that I have my model declared at the top of my cshtml page.
It is declared lke '@model [namespacehere].ViewModel.ContactViewModel'

Answer

I think MVC 4 does not support TagHelpers since they were introduced in MVC 6.

Check this out.

You could use good old

@Html.TextBoxFor(m => m.{Property})

and

@Html.ValidationMessageFor(m => m.{Property})

Another solution is to set "name" tag of your inputs to the name of the property. Ex.:

<input name="Subject" class="form-control" />