Model.IsValid总是返回true [英] Model.IsValid always returning true
问题描述
好吧,我附近的智慧在这里结束。我有一个简单的MVC3应用程序与视图模型
Ok I am near wits end here. I've got a simple MVC3 application with a viewmodel
视图模型
public class TicketViewModel {
public Ticket Ticket { get; set; }
[DisplayName("Name")]
[Required(ErrorMessage = "Requestor's name is required.")]
public string Name { get; set; } }
控制器
[HttpPost]
public ActionResult Create(TicketViewModel vm)
{
if(ModelState.IsValid) {
TempData["message"] = "Your ticket has been submitted.";
TempData["message-class"] = "success";
return RedirectToAction("Index");
}
TempData["message-class"] = "error";
return View("Index", vm);
}
由于某些原因ModelState.IsValid通过为真正所有的时间到来。即使名称为空白。这就像模型/视图模型没有验证的。所以我pretty肯定我不会钩住东西这适用于其他应用程序。我已经得到了所有的JavaScript包括在内,尽管我不认为这是问题,现在的验证。
For some reason ModelState.IsValid is coming through as true all the time. Even when Name is left blank. It's like the model/viewmodel isn't validating at all. This works on other applications so I'm pretty sure I'm not hooking up something. I've got all the validation javascript included as well though I don't think that's the problem right now.
更新
有趣的是,)正在由@ Html.TextBoxFor(生成的HTML标签不包括数据-VAL和数据VAL-需要的属性。
Update Interestingly enough, the html tags that are being generated by @Html.TextBoxFor() are NOT including the data-val and data-val-required attributes.
查看
@model MyApp.ViewModels.TicketViewModel
@{
ViewBag.Title = "Tickets";
}
<div id="main-content">
<section class="large">
<div class="section">
<div class="section-header">Submit Ticket</div>
<div class="section-content">
<div class="message"></div>
@using( Html.BeginForm("Create", "Home", FormMethod.Post) ) {
<h2>User Information</h2>
<dl>
<dt>@Html.LabelFor( m => m.Name)</dt>
<dd>
@Html.TextBoxFor( m => m.Name)
@Html.ValidationMessageFor( m => m.Name)
</dd>
<dt></dt>
<dd><button>Submit</button></dd>
</dl>
}
</div>
</div>
</section>
</div>
更新II
现在好了,这很有趣。我创建了一个新的应用程序,并得到的东西基本code工作。后来,当我加入DI code到的global.asax.cs验证停止工作。特别是,当我添加
Well now this is interesting. I created a fresh app and got things working with basic code. Then when I added DI code to the global.asax.cs validations stopped working. Specifically, when I add
public void SetupDependencyInjection() {
_kernel = new StandardKernel();
RegisterServices(_kernel);
DependencyResolver.SetResolver(new NinjectDependencyResolver(_kernel));
}
和距离的Application_Start称之为()
and call it from Application_Start()
protected void Application_Start()
{
SetupDependencyInjection();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
如果我删除SetupDependencyInjection()验证开始工作。需要明确的是,DI效果很好,但它似乎要杀死验证。这种行之有效前MVC3工具更新。
if I remove SetupDependencyInjection() validations start working. To be clear, DI works well but it seems to kill validations. This worked well prior to MVC3 Tools Update.
推荐答案
我能够找到一个解决方案。看来,当你通过的NuGet的配置稍有不同安装Ninject。它配置从App_Start文件夹的应用程序。基本上我是在我的Ninject福加倍从Global.asax中调用英寸这最终导致怪异的验证问题,但应用程序的其他部分都工作。
I was able to find a solution. Seems that when you install Ninject via nuget the configuration is a little different. It configures your application from the App_Start folder. Basically I was doubling up on my Ninject-Fu calling in from global.asax. This ended up causing the weird validation issues, though the other parts of the application were working.
这篇关于Model.IsValid总是返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!