gpt4 book ai didi

c# - Razor 中的 MVC 数据注释

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

我正在尝试在我的 MVC 项目 (Mono/.NET 4.5) 中使用数据注释。我已经创建了我的模型并添加了所有适当的注释。我的 View 和 Controller 已正确连接。但是,验证似乎并没有发生。我已经尝试了所有我能找到的东西,但都无济于事。因为这是我第一次使用 Razor 和数据注释,所以我想我缺少一些设置部分,但我终其一生都找不到它。这是我的代码:

型号

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

namespace MyWebsite
{
public class RegisterViewModel : BaseViewModel
{
#region properties
[Required(ErrorMessage = "Required")]
[StringLength(50)]
[DisplayName("First Name")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Required")]
[StringLength(100)]
[DisplayName("Last Name")]
public string LastName { get; set; }
[StringLength(50)]
[DisplayName("Display Name")]
public string DisplayName { get; set; }
[Required(ErrorMessage = "Required")]
[StringLength(255)]
[DisplayName("Email Address")]
public string EmailAddress { get; set; }
#endregion

#region ctor
public RegisterViewModel ()
{

}
#endregion

}
}

Controller

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

namespace MyWebsite.Controllers
{
public class AccountController : Controller
{
public ActionResult Register()
{
ViewData ["IsComplete"] = false;
ViewData ["RequiredVouches"] = WebSettings.RequiredVouches;

return View ();
}

[HttpPost]
public ActionResult Register(FormCollection formData)
{
if (ModelState.IsValid) {
//TODO: Put the data
ViewData ["IsComplete"] = true;
}
return View ();
}
}
}

查看

@model SummerIsles.Web.RegisterViewModel
@section Styles {
<link href="~/Content/themes/base/Account.css" rel="stylesheet" type="text/css" />
}
@{
if((bool)@ViewData["IsComplete"]) {
<h1>Registration Request Complete!</h1>
<div class="page-message">
<p>
Confirmation message goes here
</p>
</div>
}
else {
<h1>Registration</h1>
<div class="page-message">
<p>
Instruction text goes here
</p>
</div>
using (Html.BeginForm()) {
@Html.ValidationSummary(false)
<fieldset>
<legend>Your Information</legend>

<div class="group column-1">
@Html.LabelFor(modelItem => @Model.FirstName)
@Html.EditorFor(modelItem => @Model.FirstName, new { htmlAttributes = new { @class="form-control" } } )
@Html.ValidationMessageFor(modelItem => @Model.FirstName)

@Html.LabelFor(modelItem => @Model.DisplayName)
@Html.EditorFor(modelItem => @Model.DisplayName, new { htmlAttributes = new { @class="form-control" } } )
@Html.ValidationMessageFor(modelItem => @Model.DisplayName)
</div>

<div class="group column-2">
@Html.LabelFor(modelItem => @Model.LastName)
@Html.EditorFor(modelItem => @Model.LastName, new { htmlAttributes = new { @class="form-control" } } )
@Html.ValidationMessageFor(modelItem => @Model.LastName)

@Html.LabelFor(modelItem => @Model.EmailAddress)
@Html.EditorFor(modelItem => @Model.EmailAddress, new { htmlAttributes = new { @class="form-control" } } )
@Html.ValidationMessageFor(modelItem => @Model.EmailAddress)
</div>

<div class="button-options">
<button id="btnSubmit" type="submit" formmethod="post" class="btn btn-danger">Submit</button>
<a id="btnCancel" href="~/" class="btn">Cancel</a>
</div>
</fieldset>
}
}
}

此外,我已将 jquery 验证脚本添加到我的布局文件中,并且还在 web.confg 中启用了客户端验证。

布局页眉

<head>
<!--some other css and such-->
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobstrusive.min.js"></script>
</head>

Web.config

<appSettings>
<!--some other stuff-->
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

基本上,当我点击提交时,它认为模型是完全有效的(ModelState.IsValid 在 Controller 中返回 true)并且验证内容(我希望在回发到 Controller 之前触发)似乎永远不会触发.即使使用完全空白的表单(尽管有“必需”数据注释),我也没有收到任何验证消息。我错过了什么?

最佳答案

@Html.ValidationMessageFor(modelItem => @Model.LastName)

应该是

@Html.ValidationMessageFor(modelItem => modelItem.LastName)

对于所有您的 HtmlHelpers,包括 TextBoxFor 和 LabelFor 等。

还有

public ActionResult Register(FormCollection formData)

应该是

public ActionResult Register(RegisterViewModel model)

为了让您的服务器端 ModelState.IsValid 正常工作。

关于c# - Razor 中的 MVC 数据注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32532610/

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