I'm using Twitter Bootstrap, and trying to make my links ASP.Net MVC look nice.
However, the <i class=... in the link below, is html encoded, rather than being sent as html to the browser:
#Html.ActionLink("<i class='icon-user icon-white'></i> Create New", "Create", "", New With {Key .class="btn btn-primary"} )
Is there any way of keeping the <i class=... as html, so that the button displays correctly?
Instead of using #Html.ActionLink(), just write out the <a> tag yourself. You can use #Url.Action() to get the URL of an action for your HREF attribute.
The #Html helpers are nice, but they won't always provide the flexibility you need.
I was dealing with the same issue, but wanted to keep using a helper, because I was making an Ajax button.
I ended up with these two helper methods, one for each helper:
public static MvcHtmlString IconActionLink(this AjaxHelper helper, string icon, string text, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes)
{
var builder = new TagBuilder("i");
builder.MergeAttribute("class", icon);
var link = helper.ActionLink("[replaceme] " + text, actionName, controllerName, routeValues, ajaxOptions, htmlAttributes).ToHtmlString();
return new MvcHtmlString(link.Replace("[replaceme]", builder.ToString()));
}
public static MvcHtmlString IconActionLink(this HtmlHelper helper, string icon, string text, string actionName, string controllerName, object routeValues, object htmlAttributes)
{
var builder = new TagBuilder("i");
builder.MergeAttribute("class", icon);
var link = helper.ActionLink("[replaceme] " + text, actionName, controllerName, routeValues, htmlAttributes).ToHtmlString();
return new MvcHtmlString(link.Replace("[replaceme]", builder.ToString()));
}
Just throw them in a static class in your project, compile and you should see them (You may need to add an using statement on your page).
When using the helper you can use "icon-plus" or even "icon-plus icon-white" for the icon string.
3-Step Solution:
1. Create this HtmlExtensions class:
using System.Web.Mvc;
public static class HtmlExtensions
{
public static MvcHtmlString ActionButton(this HtmlHelper html, string linkText, string action, string controllerName, string iconClass)
{
//<i class="#lLink.IconClass"></i><span class="">#lLink.LinkText</span>
var lURL = new UrlHelper(html.ViewContext.RequestContext);
// build the <span class="">#lLink.LinkText</span> tag
var lSpanBuilder = new TagBuilder("span");
lSpanBuilder.MergeAttribute("class", "");
lSpanBuilder.SetInnerText(linkText);
string lSpanHtml = lSpanBuilder.ToString(TagRenderMode.Normal);
// build the <i class="#lLink.IconClass"></i> tag
var lIconBuilder = new TagBuilder("i");
lIconBuilder.MergeAttribute("class", iconClass);
string lIconHtml = lIconBuilder.ToString(TagRenderMode.Normal);
// build the ... tag
var lAnchorBuilder = new TagBuilder("a");
lAnchorBuilder.MergeAttribute("href", lURL.Action(action, controllerName));
lAnchorBuilder.InnerHtml = lIconHtml + lSpanHtml; // include the <i> and <span> tags inside
string lAnchorHtml = lAnchorBuilder.ToString(TagRenderMode.Normal);
return MvcHtmlString.Create(lAnchorHtml);
}
}
2. Add this using at your View
#using Extensions
3. And simple call when you need
#: <li class="btn btn-mini btn-inverse"> #Html.ActionButton(lLink.LinkText, lLink.ActionName, lLink.ControllerName, lLink.IconClass)</li>
Use TwitterBootstrapMVC.
It works with intellisense, allows for fluent syntax and you can write something like this with it:
#(Html.Bootstrap().ActionLinkButton("Create New", "Create")
.IconPrepend(Icons.user, true)
.Style(ButtonStyle.Primary))
Parameter true in the IconPrepend is for white icon type.
Besides, it's got a lot more very useful helpers.
Disclaimer: I'm the author of TwitterBootstrapMVC
Use of this library for Bootstrap 3 is not free.
#Html.ActionLink(" New", "Create", "", new { #class="icon"} )
In css style:
.icon:before{
font-family: FontAwesome;
content: "\f055";
}
Index
#Html.ActionLink("Link Title", "ActionName", "ControllerName", New With {.id = Model.id }, New With {.class = Html.Raw("btn btn-primary btn-mini")})
This HTML.AcionLink overload allows you to add attributess to the rendered html - remember to pass null/nothing for the needed parameters in this overload.
For any ASP.NET user this is how it worked for me:
<%: Html.ActionLink("Cancel", "Index", "Catalog_Users", new { #class = "btn btn-primary" })%>
The shown text will be: Cancel.
The ActionResult will be Index and Catalog_Users is the controller.
Though this question is old, but I still though of providing a simpler solution. At first, I thought its so difficult to achieve this but its so simple.
<i class="fa fa-fw fa-bar-chart-o"></i> Chart
#Url.Action() give the same power as that of Html.ActionLink. See below image (top button is exactly the way you're using it, while the bottom button is the solution to the problem.
I like G Jeny Remirez's answer. I just want to expand on this a bit. It's important that you use a four argumnent actionlink to include the class. So, if you just want to redirect to the same controller, but a different action, it's:
#Html.ActionLink("cancel", "Index", null, new { #class = "btn" })
Or if you want to redirect to the same controller with some parameters:
#Html.ActionLink("Cancel", "Index", new { param1 = ViewBag.param1, input = "activity" }, new { #class = "btn" })
Related
Does anyone have a simple way of adding a css class to a html label when validation fails, preferably from within the model, in the public IEnumerable Validate(ValidationContext context) override, not with jQuery or in the Controller.
I have my validationsummary giving me the error message I just want to put * next to the failed input and make its label text bold and red.
#Html.LabelFor(model => model.Name)
<div class="editor-field">
#Html.EditorFor(model => model.Name)<br/><br />
</div>
If you have not yet found a solution, look at http://weblogs.asp.net/imranbaloch/archive/2010/07/03/asp-net-mvc-labelfor-helper-with-htmlattributes.aspx
It codes an HTML Helper extension to LabelFor that supports html attributes. You could use this code as a template to modify for your needs. One option would be to detect whether a validation error has occured. A few days ago I wrote something similar:
public static string IsInvalidFor<TModel, TValue>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TValue>> expression,
string cssErrorClass)
{
if (ValidationExtensions.ValidationMessageFor(htmlHelper, expression) != null)
return cssErrorClass;
else return "";
}
if you want to do it in .cs file Model in this case just append this
string name = //ur name property//;
oppdesc = "";
oppdesc += "<span class ="error"+ "\">" + name+ "</span>";
and u define class error as bold and red in ur css.
In my application I have a lot of UI elements (particuarly buttons) where I am repeating the same code over and over.
For example in multiple views I might have an edit button that is created with the following code:
#Html.ActionLink("Edit", "Edit", "SomeController", null, new { #class="button large blue" });
The problem here is that I am hard coding the label and styling, so if I want to make changes to the button styling or label, I would have to make them in multiple views. This would be tedious to do, as I would have to track down every button.
So now I am looking at creating a templating mechanism, where I can define a button template in a central area and bring it in to any view I want to use it in.
I have considered two options, which I have tried to sketch out in semi-real world code below.
My question is, am I on the right track here? Which option would be better and for which reasons? Is there already something out there I could consider using, or is there another way which I haven't thought of?
Thanks for your help.
Web.Config Templates
Create a custom class that can bring in template configuration from web.config file. For example:
<ui.HtmlTemplates>
<add templateKey="FormCancel" tag="a" class="form-button large black" />
<add templateKey="FormSave" tag="input" type="submit" class="form-button large green" />
</ui.HtmlTemplates>
And then could call them in with syntax such as this (method signature is contrived)
#HtmlTemplates.Build("FormCancel", Url.Action("Index", "Home"))
Partial View Templates
Create strongly typed partial views with the template I want.
ViewModel
public class UiButtonModel
{
public string Url{ get; set; }
}
Partial View
// Assume the file is called "_Button_FormCancel"
#Model path.to.model.directoy.UiButtonModel
Cancel
Use
#Html.Partial("_Button_FormCancel", new UiButtonModel(){Url = Url.Action("Index", "Home"));
Another option is to create extension methods off of HtmlHelper to create prepackaged HTML output using code:
using System.Web.Mvc;
using System.Web.Routing;
public static class MyHtmlExtensions
{
public static string SaveButton(this HtmlHelper helper, string title = "Save", IDictionary<string, object> htmlAttributes = null)
{
var builder = new TagBuilder("button");
builder.Attributes.Add("type", "button");
builder.AddCssClass("form-button");
builder.AddCssClass("large");
builder.AddCssClass("green");
if (htmlAttributes != null) builder.MergeAttributes(htmlAttributes);
builder.SetInnerText(helper.Encode(title));
return builder.ToString();
}
public static string CancelButton(this HtmlHelper helper, string title = "Cancel", string actionName, string controllerName, RouteValueDictionary routeValues = null, IDictionary<string, object> htmlAttributes = null)
{
var urlHelper = new UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection);
var builder = new TagBuilder("a");
builder.Attributes.Add("href", urlHelper.Action(actionName, controllerName, routeValues));
builder.AddCssClass("form-button");
builder.AddCssClass("large");
builder.AddCssClass("green");
if (htmlAttributes != null) builder.MergeAttributes(htmlAttributes);
builder.SetInnerText(helper.Encode(title));
return builder.ToString();
}
}
Then just make sure the namespace of MyHtmlExtensions is either added to your page directly, or included in all pages via web.config, and use it like this in your view (razor syntax):
<div class="form-buttons">
#Html.CancelButton("Index", "Home")
#Html.SaveButton()
</div>
This method is particularly well suited for creating output consistently across several solutions, as all you need to do is reference the containing assembly and import the namespace.
I create these kinds of templates and put them in my Views/Shared folder.
I have templates like:
AddButton.cshtml
DeleteButton.cshtml
SaveButton.cshtml
...
Then, when I need to call one of them in whatever View, I just call this for example:
#Html.Partial("SaveButton");
Using T4MVC, it gets even better with compile time checking (no more literal strings):
#Html.Partial(MVC.Shared.Views.SaveButton)
Doing so I have a common/central place to change a specific button config. No need to go view after view to change something.
This is the problem that css was designed to handle. I fail to understand the problem. If you want to make changes, you change the CSS and it affects all the buttons that have that styling.
Part of your problem is that you're using style like "blue". If you want to change it to red, you have to change it everywhere.
Instead, you should have a class for the button, then you can simply change the button style and you don't have to worry about redefining blue to red.
I'm currently using ValidationMessageFor to locate and display any errors in my asp.net MVC3 form. To provide this on a client-side level, I'm also using JQuery's unobtrusive validation.
These methods are fantastic but they seem to be lacking one thing - an option to display an initial 'hint' when the user is in a form field, just like on Twitter's sign up form. To clarify, I'd like this to appear in the same place as the validation message.
Can anyone tell me if this functionality is available and if not, how I would go about implementing it?
Just elaborating on my comment above
The way I implemented this was by creating a "HintFor" html extension
public static MvcHtmlString HintFor<TModel, TValue>(this HtmlHelper<TModel> self, Expression<Func<TModel, TValue>> expression)
{
dynamic attribute = ModelMetadata.FromLambdaExpression(expression, self.ViewData);
return MvcHtmlString.Create(attribute.Description);
}
which just displays what you set the description property on the Display attribute for that viewmodel property
[Display(Name = "Application Title", Description = "Description goes in here")]
public string Title { get; set; }
Then in my front end I formatted each form step as shown (the extra classes are because I'm using the uni-form library to format my form)
<div class="question">
#Html.LabelFor(m => m.Title, new { #class = "label" })
<div class="formHint">
#Html.HintFor(m => m.Title)
</div>
<div class="response">
#Html.TextBoxFor(m => m.Title, new { #class = "textInput", autocomplete = "off" })
</div>
<div class="sidetip">
#Html.ValidationMessageFor(m => m.Title, null, new { #class = "invalid" })
</div>
</div>
Then using a little CSS I set the "formhint" class to display none when the error message is shown
.validation-summary-valid, .field-validation-valid
{
display: none;
}
.input-validation-error + .formHint
{
display: none !important;
}
Hope this helps
I am wondering why the following code:
#Html.DropDownList("Classification.Nationality.NationalityId", Model.Nationalities, new { #size = 10, #style = "display:none;", #class = "pickList" })
produces the following html, specifically why the name of the element is not "Classification.Nationality.NationalityId".
<select style="display: none;" size="10" name="CollectionCategory.Classification.Nationality.NationalityId" id="CollectionCategory_Classification_Nationality_NationalityId" class="pickList">
where the function signature sure looks like this:
public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, object htmlAttributes);
It seems like the name parameter gots overriden by view model of the parent view. ( This is in a partial view). Does this make sense to anyone?
It's because you are calling this helper inside an editor template or partial for a navigational property called CollectionCategory. It's perfectly normal behavior and ensures that proper value is sent to the controller action when binding. Also I would recommend you using the strongly typed version of this helper to avoid those refactor unfriendly magic strings:
#Html.DropDownListFor(
x => x.Classification.Nationality.NationalityId,
Model.Nationalities,
new {
#size = 10,
#style = "display:none;",
#class = "pickList"
}
)
Of course if you don't want to follow conventions (no idea why wouldn't you) but you could specify a binding prefix in your POST action:
[HttpPost]
public ActionResult Foo([Bind(Prefix = "CollectionCategory")] ClassificationViewModel model)
{
...
}
If I have an HTML helper like so:
Name:<br />
<%=Html.TextBox("txtName",20) %><br />
How do I apply a CSS class to it? Do I have to wrap it in a span? Or do I need to somehow utilize the HtmlAttributes property of the helper?
You can pass it into the TextBox call as a parameter.
Name:<br/>
<%= Html.TextBox("txtName", "20", new { #class = "hello" }) %>
This line will create a text box with the value 20 and assign the class attribute with the value hello. I put the # character in front of the class, because class is a reserved keyword. If you want to add other attributes, just separate the key/value pairs with commas.
This is how to add a class and a style on the same element...
"x" being the model passed to the view with a property of TextBoxID
#Html.TextBoxFor(x => x.TextBoxID, new { #class = "SearchBarSelect", style = "width: 20px; background-color: green;" })
I did some research and came across this article that seems to have a solution to your question.
Ajax Control Toolkit with ASP.NET MVC#
source: jimzimmerman
ARTICLE LINK
http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=330
QUOTE
So basically if you put the class name
TextboxWatermark on any textbox input
with the title you like to show as the
watermark like this:
<input type="text" class"TextboxWatermark" name="username" id="username" title="Must be at least 6 chars" />
or
<%= Html.TextBox("username", new { #class = "TextboxWatermark", #title = "Must be at least 6 chars" }) %>
What is nice about the second option
is that you get the added benefit of
getting the View Engine to fill out
the value of the textbox if there is
an item in ViewData of the
ViewData.Model that has a var named
'username'.
Use the htmlAttributes parameter with an anonymous type, like tihs:
<%=Html.TextBox("txtName","20", new { #class = "test"}) %>
the helper implementation
public static class LabelExtensioncs
{
public static MvcHtmlString Alarm(this HtmlHelper helper, string target, string text)
{
return MvcHtmlString.Create(string.Format("<p class='alert' style='background-color: #b8f89d;border-radius: 5px;width: 100%;'><b>{0}</b><br /><i>{1}</i></p>", target, text));
}
}
the usage in view section
#Html.Alarm("Title", "please unsure your card no is invisible in your authorized information")
the result
Theres no need to use span, because its not dynamic.
Css:
.testClass {
color: #1600d3;
}
View (Index):
#Html.TextBox("expression", "Text to show.", new { #class = "testClass" })
if you need dynamic options you can use for example:
CSS:
.test class{
background: #ffffff;
}
Controller (Index for test):
[HttpGet]
public ActionResult Index()
{
ViewBag.vbColor = "#000000";
return View();
}
View (Index):
<div>
<span>
#Html.TextBox("expression", "Text to show.", new
{ #class = "testClass", #style="color: " +
#ViewBag.vbColor })
</span>
</div>
Hope it helps.
Is it that much more work?