开发者

ASP.NET MVC Add ModelState error with a hyperlink

I have an item creation action where I check if item with the same name already exists in the system. I would like to add an error message with hyperlink (smth like 'An item with the same name already exists, you can open it via [url]') to ModelState.开发者_高级运维 My url is not static (it is not http://google.com like in example below :)), it depends on user input.

I tried smth like

        ModelState.AddModelError("Name", "http://google.com");
        ModelState.AddModelError("Name", "<a href=\"http://google.com\">http://google.com</a>");

but everything is added as a simple text.

Can anyone help me to achieve my goal? :) Thanks in advance


You might try setting a ViewBag property that can be used in your view to conditionally display the link.

ViewBag.ErrorLink = "http://google.com";

Then in your view:

if (ViewBag.ErrorLink != null)
{
    <a href="@ViewBag.ErrorLink">@ViewBag.ErrorLink</a>
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜