I need to wrap the Validation Summary in a div. How do I set the Validation Summary to wrap it with a div when errors are present?
<div class="validation-summary">
<%= Html.ValidationSummary("Login was unsuccessful. Please correct the errors and try again.") %>
</div>
I had to extend the validation summary extensions in another project of mine to deal with more than one form on a page.
Although this is different, you could create your own extension method...
namespace System.Web.Mvc
{
public static class ViewExtensions
{
public static string MyValidationSummary(this HtmlHelper html, string validationMessage)
{
if (!html.ViewData.ModelState.IsValid)
{
return "<div class=\"validation-summary\">" + html.ValidationSummary(validationMessage) + "</div>"
}
return "";
}
}
}
Then just call
<%= Html.MyValidationSummary(
"Login was unsuccessful. Please correct the errors and try again.") %>
HTHs,
Charles
What you can do is this :
<%if (!ViewData.ModelState.IsValid) { %>
<div class="validation-summary">
<%= Html.ValidationSummary(
"Login was unsuccessful. Please correct the errors and try again.") %>
</div>
<% } %>
For MVC 2, ValidationSummary is a extension method, you must add
using System.Web.Mvc.Html;
Use this CSS for li tag for example...
.validation-summary-errors ul li {color:Red;}
Related
Given this markup in an ascx file :
<div class="DocumentPara">
<%#Eval("Content1").ToString%>
<%#Eval("Content2").ToString%>
</div>
Is there a syntax I can use to chose the display of "Content1" and "Content2" depending of which masterpage is calling?. I.e. :
<div class="DocumentPara">
<%#Eval("Content1").ToString%>
<If masterpage1>
<%#Eval("Content2").ToString%>
</endIf>
<If masterpage2>
<%#Eval("Content3").ToString%>
</endIf>
</div>
Thanks for your help.
Page's masterpage can be accessed via Master property, so to check for a specific masterpage in use you can do something like
if (this.Master is Master1Type)
The if/else syntax is also possible, and will look like this:
<% if (this.Master is Master1Type) { %>
<%#Eval("Content2").ToString%>
<% }
else { %>
<%#Eval("Content3").ToString%>
<% } %>
However that looks dirty to me, and having conditionals likes this inside the page markup is not a common practice. I would suggest defining a function in code behind to deal with masterpages logic, and output necessary value from the data item:
<%# GetContent(Container.DataItem) %>
protected string GetContent(object dataItem)
{
if (this.Master is Master1Type)
{
return Eval("Content2");
}
// etc
}
Hopefully this won't be a difficult question for someone to answer, but I am having a lot of trouble finding the solution online. I am trying to add some HTML to my asp.net page from the code behind (It's VB.net). I would like to add the HTML into the head section of my page but can only add to the body currently.
You can put code in the head, just like the body. For example:
<%= CallAMethodThatReturnsAStringOfHtml() %>
You could try creating a property in your code behind and add your html in the Page_Load method:
Public MyHtml As String
then in the head section of your HTML just use the literal notation:
<%= MyHtml %>
Have runat attribute on your head element and you will be able to access it
<head id="someHead" runat="server">
</head>
Now in your codebehind, you can set it like
someHead.InnerHtml="<script src='somelibrary.js' ></script>";
I made this way, and it worked:
on the .aspx file:
...
<%
Response.Write(GetDisclosureText());
%>
...
on the aspx.cs file:
protected string GetDisclosureText()
{
string disclosure = "";
// ...Apply custom logic ...
if (!string.IsNullOrEmpty(disclosure))
{
return disclosure;
}
return "Error getting Disclosure Text";
}
Note the only difference is that I call Response.Write, not just the function.
I'm sure this is trivial, but why isn't the Windows Authentication user printing in my ASP.NET page inline?
Code behind function:
public string GetCurrentUserWindowsLogin()
{
string windowsLogin = Page.User.Identity.Name;
int hasDomain = windowsLogin.IndexOf(#"\");
if (hasDomain > 0)
{
windowsLogin = windowsLogin.Remove(0, hasDomain + 1);
}
return windowsLogin;
}
Inline code:
<div class="loginDisplay">[ <%#GetCurrentUserWindowsLogin() %> ]</div>
The <%#... %> is used for Binding Expressions like Eval and Bind.
So if you call Page.DataBind() in page_load it should work.
Another way that should work is to use code render blocks which run normal code:
<% GetCurrentUserWindowsLogin() %>
or the <%= %> construct used for small chunks of information:
<%= GetCurrentUserWindowsLogin() %>
Just a follow up on the above answer, the <%= is like response.write.
http://msdn.microsoft.com/en-us/library/vstudio/6dwsdcf5(v=vs.100).aspx
I use this code to return an string from my action :
public ActionResult Index()
{
string List = "";
List = "<table><tr><td>Ali</td></tr></table>";
ViewData["List"] = List;
return View();
}
and this is my view :
<body>
<%: ViewData["List"] %>
</body>
but instead to create a table when i browse the page , i see that the string "<table><tr><td>Ali</td></tr></table>" wrote on it. I use the firebug and see its html code . but i saw something like this :
<table><tr><td>Ali</td></tr></table>
Is there any body out there to help me?
Thansk
Regards
This is because you use the HtmlEncode tag
Change the <%: to <%=
will be come
<body>
<%= ViewData["List"] %>
</body>
and do not forget to HtmlEncode your data only to avoid any injection.
I'm ramping up on ASP.NET MVC and looking at how I output messages in the view. What's the best way to do something like this? Helpers? Controls? Or just as is?
<% if (ViewData.ContainsKey("message") && !string.IsNullOrEmpty(ViewData["message"].ToString())) { %>
<div class="notice">
<%= ViewData["message"] %>
</div>
<% } %>
I would use an html helper:
public static class HtmlExtensions
{
public static string GetMessage(this HtmlHelper htmlHelper)
{
var message = htmlHelper.ViewData["message"] as string;
if (string.IsNullOrEmpty(message))
{
return string.Empty;
}
var builder = new TagBuilder("div");
builder.AddCssClass("notice");
builder.SetInnerText(message);
return builder.ToString();
}
}
and in the view:
<%= Html.GetMessage() %>
Remark: don't forget to html encode the value of the message if you decide to use your code the way it is.
I think a condition like what you have is the simplest approach. A control feels a bit too "heavy" for this. Maybe a helper method if you find yourself repeating it a lot.