ASP.NET MVC 数据注释工具提示

发布于 2024-10-16 05:26:07 字数 138 浏览 5 评论 0原文

当表单验证失败时,我被要求显示工具提示而不是纯文本。我们当前使用 asp.net MVC 3 数据注释验证器来显示验证错误消息。我对 MVC 还很陌生,我花了几个小时在网上寻找一个干净的解决方案。如果有人能指出我正确的方向,我一定会很感激。

谢谢

I have been asked to display tooltips instead of plain text when form validation fails. We are currently using asp.net MVC 3 data annotation validators to display validation error messages. I am still fairly new to MVC and I have spent hours online looking for a clean solution. If someone could point me in the right direction I would surely appreciate it.

Thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缺⑴份安定 2024-10-23 05:26:07

您可以指定要应用于控件的 html 属性。这是使用创建控件的 HtmlHelper 方法的第二个参数来完成的。例如,在 MVC 3 中,如果您想要一个带有工具提示的文本框,当您将鼠标悬停在该文本框上时,会出现该工具提示,请使用像这样的 html title 属性。

@Html.TextBoxFor(model => model.Name, new { @class = "form", @title= "Your name as you would like it to appear in the notification email" })

要在服务器端代码中使用控制器中的值,您可以使用 ViewBag(或 MVC2 中的 ViewData)。所以代码看起来像这样:

[HttpPost]
public void Form(Model m)
{
    if(m.Name.Length==0)
        ViewBag.NameError = "Please enter your name";
}

视图代码看起来像这样

@Html.TextBoxFor(model => model.Name, new { @class = "form", @title= (ViewBag.NameError==null?string.empty:(string)ViewBag.NameError)})

You can specify the html attributes that you want to apply to your control. This is done using the second parameter of your HtmlHelper method that creates the control. For example in MVC 3 if you wanted a textbox with a tooltip that appears when you hover over it, use the html title attribute like this.

@Html.TextBoxFor(model => model.Name, new { @class = "form", @title= "Your name as you would like it to appear in the notification email" })

To use a value from your controller in the server side code you can use the ViewBag (or ViewData in MVC2). So the code would look something like this:

[HttpPost]
public void Form(Model m)
{
    if(m.Name.Length==0)
        ViewBag.NameError = "Please enter your name";
}

and the view code would look like this

@Html.TextBoxFor(model => model.Name, new { @class = "form", @title= (ViewBag.NameError==null?string.empty:(string)ViewBag.NameError)})
依 靠 2024-10-23 05:26:07

您已经完成了大部分工作,因为页面上已经显示了验证错误。您需要了解的是如何使用一些客户端脚本(javascript)才能将它们呈现为浏览器中的工具提示。

看看以下 JQuery 插件,它们可能正是您所需要的:
http://jquery.bassistance.de/tooltip/demo/

You are most of the way there as you already have the validation errors shown on the page. What you need to be looking at is how to use some client-side scripts (javascript) to be able to render these as a tooltip in the browser.

Take a look at the following JQuery plugins which may be what you are after:
http://jquery.bassistance.de/tooltip/demo/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文