ASP.NET MVC 4 ModelState.IsValid始终为false

时间:2016-08-25 02:04:18

标签: c# asp.net asp.net-mvc twitter-bootstrap asp.net-mvc-4

我意识到之前已经问过这个问题,但我没有在任何地方看到我的问题的解决方案。所以,我正在努力熟悉MVC。我正在使用MVC 4,带有bootstrap。我正在尝试创建一个联系表单。我正在关注一个教程,并在下面得到了这个。

这是viewmodel数据:

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; }

}

这是cshtml数据:

<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>

这是控制器数据:

    [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();
    }

var'error'=

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

我的问题是代码总是说ModelState.IsValid总是假的。为什么会这样?谢谢。

编辑: 我忘了提到我在我的cshtml页面顶部声明了我的模型。 它被声明为@model [namespacehere].ViewModel.ContactViewModel

1 个答案:

答案 0 :(得分:2)

我认为MVC 4不支持TagHelpers,因为它们是在MVC 6中引入的。

检查this

你可以用好旧的

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

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

另一种解决方案是将输入的“name”标记设置为属性的名称。例如:

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