在 ASP.NET MVC 中绑定视图模型作为联系人窗体
本文关键字:模型 联系人 窗体 视图 绑定 ASP NET MVC | 更新日期: 2023-09-27 17:57:03
我已经很久没有构建 ASP.NET MVC 网站了,所以我不确定我的视图模型哪里出了问题?我正在尝试为网站构建一个联系表单,但是当我单击"提交"时,表单将完全忽略JavaScript验证以及if(ModelState.IsValid)语句?!
我不确定是否是因为我没有将模型传递到我的控制器 POST 方法中?
如果有人在MVC方面有更多经验 ASP.NET 我真的很感激。
谢谢!
我的观点:
@model ThePines.ViewModels.EnquiryForm
@using (Html.BeginForm("Index", "Enquiries", FormMethod.Post, new { enctype = "multipart/form-data"}))
{
@Html.ValidationSummary(true)
<fieldset>
<table cellpadding="8" cellspacing="8">
<tr>
<td>@Html.LabelFor(model => model.FirstName, "First Name")</td>
<td>@Html.TextBoxFor(model => model.FirstName, new { @class = "medium" })</td>
<td>@Html.ValidationMessageFor(model => model.FirstName)</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.LastName, "Last Name")</td>
<td>@Html.TextBoxFor(model => model.LastName, new { @class = "medium" })</td>
<td>@Html.ValidationMessageFor(model => model.LastName)</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.EmailAddress, "Email Address")</td>
<td>@Html.TextBoxFor(model => model.EmailAddress, new { @class = "long" })</td>
<td>@Html.ValidationMessageFor(model => model.EmailAddress)</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.Country, "Country")</td>
<td>@Html.TextBoxFor(model => model.Country, new { @class = "medium" })</td>
<td>@Html.ValidationMessageFor(model => model.Country)</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.PartySize, "Party Size")</td>
<td>@Html.DropDownListFor(model => model.PartySize, new SelectList(ViewBag.PartySizes), "Party Size")</td>
<td>@Html.ValidationMessageFor(model => model.PartySize)</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.ArrivalDay, "Arrival Date")</td>
<td>@Html.DropDownListFor(model => model.ArrivalDay, new SelectList(ViewBag.ArrivalDay), "Day") @Html.DropDownListFor(model => model.ArrivalMonth, new SelectList(ViewBag.ArrivalMonth), "Month") @Html.DropDownListFor(model => model.ArrivalYear, new SelectList(ViewBag.ArrivalYear), "Year")</td>
<td>@Html.ValidationMessageFor(model => model.ArrivalDay) @Html.ValidationMessageFor(model => model.ArrivalMonth) @Html.ValidationMessageFor(model => model.ArrivalYear)</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.DepartureDay, "Departure Date")</td>
<td>@Html.DropDownListFor(model => model.DepartureDay, new SelectList(ViewBag.DepartureDay), "Day") @Html.DropDownListFor(model => model.DepartureMonth, new SelectList(ViewBag.DepartureMonth), "Month") @Html.DropDownListFor(model => model.DepartureYear, new SelectList(ViewBag.DepartureYear), "Year")</td>
<td>@Html.ValidationMessageFor(model => model.DepartureDay) @Html.ValidationMessageFor(model => model.DepartureMonth) @Html.ValidationMessageFor(model => model.DepartureYear)</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.Question, "Question")</td>
<td>@Html.TextAreaFor(model => model.Question, new { @rows = "5", @cols = "35" })</td>
<td>@Html.ValidationMessageFor(model => model.Question)</td>
</tr>
</table>
<br />
<input type="submit" value="Send Enquiry" style="padding: 5px 10px;" />
</fieldset>
}
我的视图模型:
namespace ThePines.ViewModels
{
public class EnquiryForm
{
[Required(ErrorMessage = "* Please enter a first name")]
public string FirstName { get; set; }
[Required(ErrorMessage = "* Please enter a last name")]
public string LastName { get; set; }
[Required(ErrorMessage = "* Please enter an email address")]
[EmailAddress(ErrorMessage = "* Please enter a valid email address")]
public string EmailAddress { get; set; }
[Required(ErrorMessage = "* Please enter a country")]
public string Country { get; set; }
[Required(ErrorMessage = "* Please enter a party size")]
public int PartySize { get; set; }
[Required(ErrorMessage = "* Please enter an arrival day")]
public int ArrivalDay { get; set; }
[Required(ErrorMessage = "* Please enter an arrival month")]
public int ArrivalMonth { get; set; }
[Required(ErrorMessage = "* Please enter an arrival year")]
public int ArrivalYear { get; set; }
[Required(ErrorMessage = "* Please enter a departure day")]
public int DepartureDay { get; set; }
[Required(ErrorMessage = "* Please enter a departure month")]
public int DepartureMonth { get; set; }
[Required(ErrorMessage = "* Please enter a departure year")]
public int DepartureYear { get; set; }
[Required(ErrorMessage = "* Please enter a question")]
public string Question { get; set; }
}
}
我的控制器:
using ThePines.ViewModels;
namespace ThePines.Controllers
{
public class EnquiriesController : Controller
{
//
// GET: /Enquiries/
public ActionResult Index()
{
ViewBag.PartySizes = Enumerable.Range(1, 8);
ViewBag.ArrivalDay = Enumerable.Range(1, 31);
ViewBag.ArrivalMonth = Enumerable.Range(1, 12);
ViewBag.ArrivalYear = Enumerable.Range(DateTime.Now.Year, 3);
ViewBag.DepartureDay = Enumerable.Range(1, 31);
ViewBag.DepartureMonth = Enumerable.Range(1, 12);
ViewBag.DepartureYear = Enumerable.Range(DateTime.Now.Year, 3);
return View();
}
// POST: /Enquiries/
[HttpPost]
public ActionResult Index(FormCollection enquiryForm)
{
ViewBag.PartySizes = Enumerable.Range(1, 8);
ViewBag.ArrivalDay = Enumerable.Range(1, 31);
ViewBag.ArrivalMonth = Enumerable.Range(1, 12);
ViewBag.ArrivalYear = Enumerable.Range(DateTime.Now.Year, 3);
ViewBag.DepartureDay = Enumerable.Range(1, 31);
ViewBag.DepartureMonth = Enumerable.Range(1, 12);
ViewBag.DepartureYear = Enumerable.Range(DateTime.Now.Year, 3);
if (ModelState.IsValid)
{
StringBuilder message = new StringBuilder();
message.Append("Name: " + enquiryForm["FirstName"] + " " + enquiryForm["LastName"] + "'n");
message.Append("Email Address: " + enquiryForm["EmailAddress"] + "'n");
message.Append("Country: " + enquiryForm["Country"] + "'n");
message.Append("Party Size: " + enquiryForm["PartySize"] + "'n");
message.Append("Arrival Date: " + enquiryForm["ArrivalDate"] + "/" + enquiryForm["ArrivalMonth"] + "/" + enquiryForm["ArrivalYear"] + "'n");
message.Append("Departure Date: " + enquiryForm["DepartureDate"] + "/" + enquiryForm["DepartureMonth"] + "/" + enquiryForm["DepartureYear"] + "'n");
message.Append("Questions: " + enquiryForm["Questions"]);
MailMessage mail = new MailMessage();
SmtpClient smtpServer = new SmtpClient(ConfigurationManager.AppSettings["SendMailSmtp"]);
mail.From = new MailAddress(ConfigurationManager.AppSettings["SendMailFrom"]);
mail.To.Add(ConfigurationManager.AppSettings["SendMailTo"]);
mail.Subject = "The Pines Enquiry";
mail.ReplyToList.Add(enquiryForm["EmailAddress"]);
mail.Body = message.ToString();
smtpServer.Send(mail);
}
return View(enquiryForm);
}
}
}
您添加了动作FormCollection
参数,则需要传递ViewModel
对象,以便它检查模型状态,当前您正在发布 FormCollection 而不是ViewModel
在您的操作中,因此您的 ViewModel 不会发布,请执行以下操作:
[HttpPost]
public ActionResult Index(EnquiryForm enquiryForm)
{
if(ModelState.IsValid)
{
StringBuilder message = new StringBuilder();
message.Append("Name: " + enquiryForm.FirstName + " " + enquiryForm.LastName + "'n");
message.Append("Email Address: " + enquiryForm.EmailAddress + "'n");
message.Append("Country: " + enquiryForm.Country + "'n");
message.Append("Party Size: " + enquiryForm.PartySize + "'n");
message.Append("Arrival Date: " + enquiryForm.ArrivalDate + "/" + enquiryForm.ArrivalMonth + "/" + enquiryForm.ArrivalYear + "'n");
message.Append("Departure Date: " + enquiryForm.DepartureDate + "/" + enquiryForm.DepartureMonth + "/" + enquiryForm.DepartureYear + "'n");
message.Append("Questions: " + enquiryForm.Questions);
MailMessage mail = new MailMessage();
SmtpClient smtpServer = new SmtpClient(ConfigurationManager.AppSettings["SendMailSmtp"]);
mail.From = new MailAddress(ConfigurationManager.AppSettings["SendMailFrom"]);
mail.To.Add(ConfigurationManager.AppSettings["SendMailTo"]);
mail.Subject = "The Pines Enquiry";
mail.ReplyToList.Add(enquiryForm.EmailAddress);
mail.Body = message.ToString();
smtpServer.Send(mail);
}
return View(enquiryForm);
}
当前您正在执行此操作:
[HttpPost]
public ActionResult Index(FormCollection enquiryForm)
{
}
或者,如果您想从FormCollection
读取已发布的数据,则可以执行以下操作:
[HttpPost]
public ActionResult Index(EnquiryForm enquiryFormModel,FormCollection enquiryFormCollection)
{
}