检查modelstate.isvalid时模型无效 [英] Model is not valid when checking modelstate.isvalid
问题描述
模型无效,我无法弄清楚原因。
模型如下:
Model is not valid and I cannot figure out why.
Model is below:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace PTPA.Models
{
public class ContactUs
{
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
public string Subject { get; set; }
[Required]
public string Message { get; set; }
}
}
查看
前端位于:
View
And the front end is below:
@model PTPA.Models.ContactUs
@{
ViewBag.Title = "ContactForm";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>ContactForm</h2>
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<div>
<div class="col-md-6">
<div>
@if (ViewBag.Message == null)
{
<div>
<form method="post">
<div class="form-group">
<label asp-for="FirstName">First Name</label>
<input asp-for="FirstName" class="form-control" />
<span asp-validation-for="FirstName"
class="text-muted"></span>
</div>
<div class="form-group">
<label asp-for="LastName">Last Name</label>
<input asp-for="LastName" class="form-control" />
<span asp-validation-for="LastName"
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="10" cols="10"
asp-for="Message" class="form-control"></textarea>
<span asp-validation-for="Message"
class="text-muted"></span>
</div>
<div>
<button type="Submit"
class="btn btn-default">
Send Message
</button>
</div>
</form>
</div>
}
</div>
<div>
<div>
@if (ViewBag.Message != null)
{
<div>@ViewBag.Message</div>
}
</div>
</div>
</div>
</div>
<div>END</div>
MadMyche添加的控制器类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using PTPA.Models;
namespace PTPA.Controllers {
public class ContactController : Controller {
// GET: Contact
[HttpGet]
public ActionResult ContactForm() {
return View("ContactForm");
}
//Post : Contact
[HttpPost]
public ActionResult ContactForm(ContactUs cu) {
if (ModelState.IsValid) {
try {
MailMessage msg = new MailMessage {
From = new MailAddress(cu.Email)
};
msg.To.Add("donotreplyptpa@gmail.com");
var fromEmailPassword = "example";
msg.Subject = cu.Subject;
msg.Body = "The following has been sent by a customer from the 'contact us' page. " + cu.Message;
var smtp = new SmtpClient {
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false
};
smtp.Credentials = new System.Net.NetworkCredential("example@gmail.com", fromEmailPassword);
smtp.Send(msg);
ModelState.Clear();
ViewBag.Message = "Thank you for sending us your message, we value your response and aim to get back to you within 24 hours. Thank you.";
}
catch (Exception ex) {
ModelState.Clear();
ViewBag.Message = $" Something went wrong {ex.Message}";
}
}
return View("ContactForm");
}
public ActionResult Error() {
return View("ContactForm");
}
}
}
我尝试了什么:
我尝试过更换和切割模型,但没有任何效果,我是新手,只是练习所以任何帮助都会很棒。
更新:这些是正在使用的值
What I have tried:
I have tried changing and chopping the model but nothing has worked, i am new to this and only practicing so any help would be great.
Update: These are the values that are being used
FirstName: Tom
LastName: Allen
Email: tomAllen@gmail.com
Subject: TEST
Message: TEST
推荐答案
出了点问题{ex.Message};
}
}
返回查看( 的ContactForm跨度>);
}
public ActionResult错误(){
return 查看( ContactForm);
}
}
}
" Something went wrong {ex.Message}"; } } return View("ContactForm"); } public ActionResult Error() { return View("ContactForm"); } } }
我尝试了什么:
I have tried changing and chopping the model but nothing has worked, i am new to this and only practicing so any help would be great.
$b $b
Update: These are the values that are being used
What I have tried:
I have tried changing and chopping the model but nothing has worked, i am new to this and only practicing so any help would be great.
Update: These are the values that are being used
FirstName: Tom
LastName: Allen
Email: tomAllen@gmail.com
Subject: TEST
Message: TEST
The error typically means that yourModel
doesn't meet the requirements to be validated. In your example, yourFirstName
,LastName
,Subject
andMessage
properties are decorated with the[Required]
attribute. This means that those values shouldn’t benull
andempty
otherwise your conditionif(ModelState.IsValid)
will befalse
.
To ensure that your model captures the data, I would suggest you to use the debugger, set a break-point at yourContactForm
Action method and then step into your code. Check the variablecu
and see if all the properties have values associated on it. Here’s one reference that you can use to get started with debugging in Visual Studio: Learn to debug using the Visual Studio debugger - Visual Studio | Microsoft Docs[^]
The error typically means that yourModel
doesn't meet the requirements to be validated. In your example, yourFirstName
,LastName
,Subject
andMessage
properties are decorated with the[Required]
attribute. This means that those values shouldn't benull
andempty
otherwise your conditionif(ModelState.IsValid)
will befalse
.
To ensure that your model captures the data, I would suggest you to use the debugger, set a break-point at yourContactForm
Action method and then step into your code. Check the variablecu
and see if all the properties have values associated on it. Here's one reference that you can use to get started with debugging in Visual Studio: Learn to debug using the Visual Studio debugger - Visual Studio | Microsoft Docs[^]
Use your HTMl form inside this BeginForm.
Use your HTMl form inside this BeginForm.
@using (Html.BeginForm("MethodName", "ControllerName", FormMethod.Post))
{
}
这篇关于检查modelstate.isvalid时模型无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!