-
Notifications
You must be signed in to change notification settings - Fork 0
HtmlHelper extensions
Reusable HtmlHelper
extension methods. Remember to add <add namespace="NecroNet.Toolkit.Mvc.Html" />
to you web.config to use these.
Advantage of this method is that you can use it with only one parameter, the text of the button. Futures assembly also has similar method, but you have to specify text and a name, which is bothersome.
<%: Html.Submit("Submit this!") %>
Alternatively you can specify html attributes
<%: Html.Submit("Submit this!", new { @class="submit", id = "submit-button" }) %>
Obsoleted by Html.Raw() in mvc 3.
-If you need to display some HTML content, this is the method to use if you want to use -<%: %>
block. It will only HTML encode script tags, so no malicious code get through.
--<%: Html.Markup(Model.HtmlContent) %>
When you use built-in Html.Label()
or Html.LabelFor()
methods and you don’t have fields with appropriate ids, they will render for
attribute for the label anyway, resulting in invalid markup. This simple label renders label without the for
attribute.
<%: Html.SimpleLabel("Name") %>
<%: Html.SimpleLabelFor(m => m.Name) %>
Example of when to use this is displaying data as text with a label.
<p>
<%: Html.SimpleLabelFor(m => m.Name) %>
<%: Html.DisplayFor(m => m.Name) %>
</p>
Occasionally you have some multiple choice single answer in a form. This will render group of radio boxes from a IEnumerable<SelectListItem>
<%: Html.RadioGroupFor(m => m.FavouriteProgrammingLanguageId, Model.ProgramingLanguages) %>
Renders hidden fields for a form. This is useful if used with T4MVC when you don’t know all parameters to action method right away on the server, but you know some. For example:
<% using(Html.BeginForm(MVC.Home.Index(), FormMethod.Post) { %>
<% Html.RenderHiddenFields(new { id = Model.Id, name = Model.Name }); %>
<%: Html.TextArea("comment") %>
<% } %>
public ActionResult AddComment(int id, string name, string comment)
{
...
}