gpt4 book ai didi

c# - 验证在 MVC 4 项目中不起作用

转载 作者:行者123 更新时间:2023-11-30 22:08:45 25 4
gpt4 key购买 nike

我正在 VS2012 中制作一个示例 MVC4 项目..我想在文本框字段上进行一些验证..但不幸的是它永远不起作用,我正在发布我的文件,

我的 friend Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using validators.Models;

namespace validators.Controllers
{
public class FriendController : Controller
{
//
// GET: /Friend/

public ActionResult Create()
{

return View();

}
}
}

我的模型类person.cs

    using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace validators.Models
{
public class person
{
[Required(ErrorMessage="must")]
[StringLength(10, ErrorMessage="blah")]
public string Firstname { get; set; }
[Required]
public string Lastname { get; set; }
}
}

我正在添加强类型 View ....生成以下 create.cshtml

@model validators.Models.person

@{
ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
<legend>person</legend>

<div class="editor-label">
@Html.LabelFor(model => model.Firstname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Firstname)
@Html.ValidationMessageFor(model => model.Firstname)
</div>

<div class="editor-label">
@Html.LabelFor(model => model.Lastname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Lastname)
@Html.ValidationMessageFor(model => model.Lastname)
</div>

<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@*@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}*@

当我运行这个项目并在文本框中输入没有任何值的提交按钮时,没有验证消息....帮助是可观的

最佳答案

@{ Html.EnableClientValidation(); 在你看来。如果这有帮助,则意味着在 web.config 文件中关闭了验证。

还要验证您的脚本是否包含在下一个订单中:

  1. jquery
  2. jquery.validate.js
  3. jquery.validate.unobtrusive.js

关于c# - 验证在 MVC 4 项目中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22071660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com