我正在开发一个应用程序,我有一个表单,其中每个文本框都有验证。我已经包含了自定义验证语句。它们工作得更好。但我有一个文本框的电子邮件地址数据类型。但是当我提交表单时,我遇到错误“价值'电子邮件地址'无效”..请查看我的代码并让我知道是否有任何错误.. 我的模型类是
[Table("email")]
public class eMail
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
[Required(ErrorMessage="The Name is Required")]
[StringLength(20,ErrorMessage="The maximum length of the name should be 20 Characters")]
[Display(Name="Your Name")]
public string Name { get; set; }
[Required(ErrorMessage = "Email is required")]
[EmailAddress(ErrorMessage = "Enter a proper email address")]
[StringLength(25, ErrorMessage = "Maximum length allowed for an email is 30 characters")]
public string Email { get; set; }
[Required(ErrorMessage="Please print your request")]
[StringLength(200,ErrorMessage="Maximum Characters allowed Are 200")]
[Display(Name="Prayer Request")]
[DataType(DataType.MultilineText)]
public string Request{get;set;}
}
我的控制器是:
using CTCFremont.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace CTCFremont.Controllers
{
public class eMailController : Controller
{
private CTCFremontEntities db = new CTCFremontEntities();
//
// GET: /eMail/
public ViewResult Index()
{
return View();
}
public ActionResult Create()
{
return View();
}
//
// POST: /eMail/Create
[HttpPost]
public ActionResult Create(eMail email)
{
if (ModelState.IsValid)
{
db.eMails.Add(email);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(email);
}
//
// GET: /eMail/Edit/5
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
我的观点是:
@model CTCFremont.Models.eMail
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<style type="text/css">
#RequestForm {
position:absolute;
top: 50%;
left: 25%;
width:30em;
height:18em;
margin-top: -9em; /*set to a negative number 1/2 of your height*/
margin-left: -15em; /*set to a negative number 1/2 of your width*/
border: 1px solid #ccc;
}
div.request {
box-shadow: 10px 10px 5px #888888
}
</style>
<h2>Submit Your Prayer Request</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div id="#Actual" style ="background-image:url('../../Images1/pray.jpg')">
<div class="request" id="RequestForm" style="width:1000px">
<div class="container">
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="text-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Request)
</div>
<div class="editor-field" >
@Html.TextAreaFor(model => model.Request,new { style = "width:400px ",maxlength = 200 })
@Html.ValidationMessageFor(model => model.Request)
</div>
<p>
<input type="submit" value="Create" />
</p>
</div>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我的表是:
CREATE TABLE [dbo].[eMail] (
[ID] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (20) NOT NULL,
[Email] NVARCHAR (25) NOT NULL,
[Request] NVARCHAR (200) NOT NULL,
PRIMARY KEY CLUSTERED ([ID] ASC)
);
答案 0 :(得分:0)
我认为原因是MVC模型绑定器在您的eMail模型类和该类的Email属性之间混淆。
您可以通过在ModelState.IsValid上设置断点来验证,并验证ModelState.Errors对象以查看出现了什么问题。
此外,您是否可以尝试将您的电子邮件属性重命名为Email1,并在其中添加Column属性以使其与您的表同步。
像这样的东西,试试:
[Column("Email")]
[Required]
[EmailAddress(ErrorMessage = "Enter a proper email address")]
[StringLength(25, ErrorMessage = "Maximum length allowed for an email is 30 characters")]
public string Email1 { get; set; }