MVC验证与自动完成场 [英] MVC validation with autocomplete field

查看:195
本文介绍了MVC验证与自动完成场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,模型验证。

I’ve a small problem with model validation.

我有一个名为theID的隐藏字段,一个名为theDesc文本框。本场theDesc是自动完成的,当我选择它,theID值存储的ID。

I have a hidden field called "theID" and a textbox called "theDesc". The field theDesc is an autocomplete, and when I select it, theID get the id stored.

theID是存储在数据库中的字段,并且是一个是需要选用,所以我用:@ Html.ValidationMessage(theID)

theID is the field that is stored in database, and is the one that is "Requiered", so I use: @Html.ValidationMessage("theID")

的问题是,CSS类输入验证误差被分配到隐藏字段。我现在这种行为是正常的和预期的,但有一种方法来改变呢?

The problem is that the CSS class input-validation-error is assigned to the hidden field. I now this behavior it’s normal and expected, but there is a way to change this?

我要完成的是,theDesc字段显示输入错误类。这种方式,用户可以注意到缺少

what I want to accomplish is that the theDesc field shows the input error class. This way the user can notice that is missing.

是不是什么大不了的事,但我的OCD是快把我逼疯了关于不相同的风格让所有的错误。

Is not a big deal, but my OCD is driving me crazy about not get all the errors with the same style.

推荐答案

很好的问题,用词得当!我有完全一样的问题,但看不出有什么明确的答案。我设法做到这一点 - 至少在服务器端验证 - 通过使用HTML佣工,而不是自定义HTML在控制器和视图中的添加服务器端验证。这篇文章让我:

Good question and nicely phrased! I had exactly the same question, but see no definitive answer yet. I managed to do it - at least with server side validation - by adding a server side validation in the controller and in the view using HTML helpers instead of custom HTML. This post helped me:

<一个href=\"http://stackoverflow.com/questions/3475556/how-asp-net-mvc-applies-input-validation-error-to-a-textbox-when-the-form-is-re\">How ASP.NET MVC应用。输入验证错误到一个文本框当表单被重新显示为失败的价值?

正如在后提到,有必要例如设定的HTML的名称正确的模型字段。所以我说在控制器中的一些服务器端验证code。注意检查现场model.theID是如何从ModelState中TheDesc补充说:名称不同:

As mentioned in the post it is necessary to set the name of the html e.g. model field correctly. So I added some server side validation code in the controller. Notice how the checked field model.theID is different from the name added in the ModelState "TheDesc":

控制器code:

public ActionResult SomeActionIndex(SomeModel model) {
   ...
   if (string.IsNullOrEmpty(model.theID) {
        ModelState.AddModelError("theDesc", "The field is uhm... required!");
   }
   ...
}

在文章的评论表明,对于突出(通过CSS类红色边框),它可以帮助使用标准的HTML佣工,因为这些自动启用使用jQuery的验证。
在我的案件initialy不使用HTML帮助,但嵌套视图里面的一些自定义的HTML生成的验证自动完成列表。我改变这一点。

The comments in the post indicate that for highlighting (red border via the CSS class) it helps to use the standard HTML helpers as these automatically enable the use of the jQuery validation. In my case the validated autocomplete list was initialy NOT generated using an HTML helper but some custom HTML inside a nested view. I changed this.

查看code:

@Html.TextBoxFor(m => Model.theDesc, new { @class="auto-complete", 
@placeholder="choose a value..." })

@Html.TextBoxFor(m => Model.theID, new { @style = "display: none" })

请注意:precisely那种黑魔法的HTML佣工做,以使这项工作,我一直没能尚未搞清楚。所以我会离开它,直到后来,还是为别人,但我认为这个问题涉及到它:

Note: Precisely the kind of black magic that the HTML helpers do to make this work I haven't been able to figure out yet. So I'll leave it till later, or for somebody else, but I think this question relates to it:

<一个href=\"http://stackoverflow.com/questions/8205646/custom-htmlhelper-with-validation-support?rq=1\">custom用的HtmlHelper验证支持

这篇关于MVC验证与自动完成场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆