.NET Core MVC 验证消息不显示错误

.NET Core MVC Validation messages not displaying on error

我在 .NET Core MVC 中有一个简单的联系我们表单,我向它添加了验证。但是错误消息没有显示。我在互联网上搜索了这个错误,但大多数搜索结果都是针对旧的 mvc我再次查看了我的代码以进行验证,但我似乎没有弄清楚我哪里出错了。

这是我的观点

<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
        <h3>Send us a Message:</h3>
        <form method="post">
            <div class="form-group">
                <label class="control-label" for="name">Name:</label>
                <input type="text" class="form-control" id="name" asp-for="name" placeholder="Enter name">
                <br />
                <span asp-validation-for="name"></span>
                <label class="control-label" for="email" asp-for="email">Email:</label>
                <input type="text" class="form-control" id="email" placeholder="Enter email" asp-for="email"><br />
                <span asp-validation-for="email"></span>
                <label class="control-label" for="phone">Phone:</label>
                <input type="text" class="form-control" id="phone" asp-for="phone" placeholder="Enter Phone"><br />
                <span asp-validation-for="phone"></span>
                <label class="control-label" for="subject">Message:</label>
                <textarea id="subject" class="form-control" asp-for="subject"></textarea>
                <span asp-validation-for="subject"></span>
            </div>
            <button type="submit" class="btn btn-dark" asp-controller="Home" asp-action="SendMailHome">Submit</button>
              @TempData["messageHome"]
            <br />
            <br />
        </form>  

我的模型

public class ContactUsModel
    {
        [BindProperty]      
        public int id { get; set; }
        [Required]
        public string name { get; set; }
        [Required]
        public string email { get; set; }
        [Required]
        public string phone { get; set; }
        [Required]
        public string subject { get; set; }

    }

我的控制器代码

public IActionResult SendMail(ContactUsModel contact1)
        {
            if (ModelState.IsValid==true)
            {
                Execute(contact1.email, contact1.name, contact1.phone, contact1.subject).Wait();
                TempData["Message"] = "Thank you for contacting us,we will respond to you shortly";
                _contactRepository.AddContactUsMail(contact1);
                return RedirectToAction("ContactUs");

            }
            else
            {
                return RedirectToAction("ContactUs",contact1);
            }

        }

重定向时,模型状态中没有可用信息。 以最简单的方式,您必须 return 查看以从框中进行工作验证。

public IActionResult SendMail(ContactUsModel contact1)
    {
        if (ModelState.IsValid)
        {
            Execute(contact1.email, contact1.name, contact1.phone, contact1.subject).Wait();
            TempData["Message"] = "Thank you for contacting us,we will respond to you shortly";
            _contactRepository.AddContactUsMail(contact1);
            return RedirectToAction("ContactUs");

        }
        else
        {
            return View("ContactUs",contact1);
        }

    }