簡體   English   中英

“值'emailaddress'無效”在asp.net mvc中驗證失敗

[英]“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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM