c# - How to include a link in AddModelError message? -



c# - How to include a link in AddModelError message? -

i want add together modelstate error, so:

modelstate.addmodelerror("", "some message, <a href="/controller/action">click here</a>)

however, link doesn't encoded, , displayed text. tried using

<%= html.validationsummary(true, "some message")

instead of

<%: html.validationsummary(true, "some message")

but no luck.

anyone have thought how working?

cheers

the validationsummary helper automatically html encodes messages. 1 possible workaround write custom validation summary helper doesn't html encode messages:

public static class htmlextensions { public static mvchtmlstring myvalidationsummary(this htmlhelper htmlhelper, bool excludepropertyerrors, string message) { var formcontext = htmlhelper.viewcontext.clientvalidationenabled ? htmlhelper.viewcontext.formcontext : null; if (formcontext == null && htmlhelper.viewdata.modelstate.isvalid) { homecoming null; } string messagespan; if (!string.isnullorempty(message)) { tagbuilder spantag = new tagbuilder("span"); spantag.setinnertext(message); messagespan = spantag.tostring(tagrendermode.normal) + environment.newline; } else { messagespan = null; } var htmlsummary = new stringbuilder(); tagbuilder unorderedlist = new tagbuilder("ul"); ienumerable<modelstate> modelstates = null; if (excludepropertyerrors) { modelstate ms; htmlhelper.viewdata.modelstate.trygetvalue(htmlhelper.viewdata.templateinfo.htmlfieldprefix, out ms); if (ms != null) { modelstates = new modelstate[] { ms }; } } else { modelstates = htmlhelper.viewdata.modelstate.values; } if (modelstates != null) { foreach (modelstate modelstate in modelstates) { foreach (modelerror modelerror in modelstate.errors) { string errortext = getusererrormessageordefault(htmlhelper.viewcontext.httpcontext, modelerror, null /* modelstate */); if (!string.isnullorempty(errortext)) { tagbuilder listitem = new tagbuilder("li"); listitem.innerhtml = errortext; htmlsummary.appendline(listitem.tostring(tagrendermode.normal)); } } } } if (htmlsummary.length == 0) { htmlsummary.appendline(@"<li style=""display:none""></li>"); } unorderedlist.innerhtml = htmlsummary.tostring(); tagbuilder divbuilder = new tagbuilder("div"); divbuilder.addcssclass((htmlhelper.viewdata.modelstate.isvalid) ? htmlhelper.validationsummaryvalidcssclassname : htmlhelper.validationsummarycssclassname); divbuilder.innerhtml = messagespan + unorderedlist.tostring(tagrendermode.normal); if (formcontext != null) { // client val summaries need id divbuilder.generateid("validationsummary"); formcontext.validationsummaryid = divbuilder.attributes["id"]; formcontext.replacevalidationsummary = !excludepropertyerrors; } homecoming mvchtmlstring.create(divbuilder.tostring()); } private static string getusererrormessageordefault(httpcontextbase httpcontext, modelerror error, modelstate modelstate) { if (!string.isnullorempty(error.errormessage)) { homecoming error.errormessage; } if (modelstate == null) { homecoming null; } string attemptedvalue = (modelstate.value != null) ? modelstate.value.attemptedvalue : null; homecoming string.format(cultureinfo.currentculture, "the value {0} invalid.", attemptedvalue); } }

and then:

<%= html.myvalidationsummary(true, "some message") %>

of course of study doing should careful text putting error messages not html encoded. means if ever wanted utilize special characters such <, >, & message need html encode or markup break.

c# .net asp.net-mvc-2

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -