【发布时间】:2014-01-31 16:19:26
【问题描述】:
我有一个 Webform 项目,我想将 MVC 5 添加到其中,以便开发新的东西是 MVC。 我喜欢在 MVC 中使用的功能之一是 Unobtrusive 客户端验证。但我似乎无法让它在这种情况下工作。验证在服务器端有效,但在客户端无效。通过检查标签,我注意到没有生成属性。我没有在浏览器控制台中显示任何 javascript 错误。 如果我在纯 MVC5 项目中执行此操作,它可以正常工作。 有谁知道我怎样才能完成这项工作?我错过了什么吗?
我一直在测试的方式是:
- 我创建了一个网络表单项目(使用框架 4.5)。
- 我从 NuGet MVC5 添加到项目中。
- 创建一个区域。
- 添加 AreaRegistration.RegisterAllAreas();到 Global.asax,以便我可以使用区域。
- 从 NuGet 添加:
- jQuery 验证
- Microsoft jQuery 非侵入式验证
- 在之前创建的区域中创建一个控制器并添加一个视图。
- 在我添加到 appSettings 的 View 文件夹的 web.config 中:
- 然后我创建一个模型并添加验证。
查看索引:
@model Merda.Areas.Branchen.Models.TestModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>TestModel</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.prop1, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.prop1)
@Html.ValidationMessageFor(model => model.prop1)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.prop2, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.prop2)
@Html.ValidationMessageFor(model => model.prop2)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
控制器:
public class TestController : Controller
{
//
// GET: /Branchen/Test/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(TestModel model)
{
if (ModelState.IsValid)
{
ModelState.AddModelError(String.Empty, "Valid");
}
else {
ModelState.AddModelError(String.Empty, "Error...");
}
return View();
}
}
已创建模型:
public class TestModel
{
[Required]
public string prop1 { get; set; }
[Required]
public string prop2 { get; set; }
}
创建的 View 文件夹内的 Web.config。
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.Optimization" />
<add namespace="Merda" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>
更新: 我正在使用 VS2012 专业版。
【问题讨论】:
标签: asp.net asp.net-mvc validation webforms asp.net-mvc-5