【发布时间】:2016-01-23 13:46:26
【问题描述】:
我正在开发一个应用程序,我有一个表单,其中每个文本框都有验证。我已经包含了自定义验证语句。它们工作得更好。但是我有一个文本框的电子邮件地址数据类型。但是当我提交表单时,我遇到错误“'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)
);
【问题讨论】:
-
您尝试使用哪个电子邮件地址?
-
sujaysiddu@gmail.com,sujay@gmail.com
-
以上电子邮件均无效
-
这里有趣的问题是您引用的错误消息没有出现在您的示例中的任何地方......您的模型错误将是
Enter a proper email address。您可能需要同时搜索源代码和可以在浏览器中找到的代码,以查看是否可以找到该消息...您提到的电子邮件应该可以通过浏览器验证type="email"和 jQuery 验证进行验证,并通过 MVC 验证......所以这里涉及到其他东西。 -
我可以说需要 MVC 验证。我已经包含了这些 jQuery 验证文件
标签: c# validation asp.net-mvc-4