[英]“the value 'emailaddress' is invalid” Validation fails in asp.net mvc
我正在开发一个应用程序,并且我有一个表单,每个文本框都包含验证信息,包括自定义验证语句,它们工作得更好,但是我有一个文本框的电子邮件地址数据类型,但是当我提交时我遇到错误“错误的值'emailaddress'的值”的表格。.请查看我的代码,并让我知道是否有错误。
[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)
);
我相信原因是MVC模型资料夹在您的电子邮件模型类和该类的Email属性之间变得混乱。
您可以通过在ModelState.IsValid上设置一个断点来验证这一点,并验证ModelState.Errors对象以查看出了什么问题。
另外,您是否可以在引用它的任何地方尝试将Email属性重命名为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; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.