gpt4 book ai didi

c# - ASP.NET MVC3 验证问题

转载 作者:太空狗 更新时间:2023-10-29 23:10:39 26 4
gpt4 key购买 nike

我有以下 ViewModel:

public class Bulletin1ViewModel
{
[Required]
public String NumberDelegations { get; set; }

[Required]
public String TravelPlans { get; set; }
}

我想在我的 View 中使用:

     @using ErasProject.Models
@model ErasProject.Models.Bulletin1ViewModel
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></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>

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

<fieldset>

<p>
@Html.EditorFor(model => model.NumberDelegations)
@Html.ValidationMessageFor(model => model.NumberDelegations)
</p>

<p>
@Html.EditorFor(model => model.TravelPlans)
@Html.ValidationMessageFor(model => model.TravelPlans)
</p>

<p>
<input type="submit" value="Submit" />
</p>

</fieldset>

}

但是我的验证没有被触发。无论是客户端还是服务器端。任何人都可以知道为什么?谢谢。

最佳答案

您需要同时添加 jquery 和 jQuery Validation 插件(以及可选的非侵入式库)

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"           type="text/javascript"></script>
<script src="http://ajax.microsoft.com/ajax/jQuery.Validate/1.7/jQuery.Validate.min.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>

因此,您的 View 将是:

@using ErasProject.Models
@model ErasProject.Models.Bulletin1ViewModel

<script src="jquery.min.js"></script>
<script src="jQuery.Validate.min.js"></script>
<script src="jquery.validate.unobtrusive.min.js"></script>

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

<fieldset>

<p>
@Html.EditorFor(model => model.NumberDelegations)
@Html.ValidationMessageFor(model => model.NumberDelegations)
</p>

<p>
@Html.EditorFor(model => model.TravelPlans)
@Html.ValidationMessageFor(model => model.TravelPlans)
</p>

<p>
<input type="submit" value="Submit" />
</p>

</fieldset>

}

刚刚添加了您的对象并创建了一个 Add 操作,例如:

public ActionResult Add()
{
return View();
}

并使用 Create 模板和 Bulletin1ViewModel 类创建了一个 View ,如下所示:

@model WebApp_MVC3.Models.Bulletin1ViewModel

@{
ViewBag.Title = "Add";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Add</h2>

<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>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Bulletin1ViewModel</legend>

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

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

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

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

什么都不做,结果是:

original file

enter image description here

我会重新检查 javascript 库...

关于c# - ASP.NET MVC3 验证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6016389/

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