html-helper

Difference between HtmlTable and TagBuilder(“table”)

。_饼干妹妹 提交于 2019-12-06 01:02:52
Just wondering what the difference is between these two and what the benefit for one or the other of them would be when I build my table in my HtmlHelper HtmlTable table = new HtmlTable(); and: TagBuilder table = new TagBuilder("table"); This is more or less the same as this question, Why use TagBuilder instead of StringBuilder? but I'm wondering more specifically about the difference between these two . The main difference is that HtmlTable provides typed and correctly named properties for all the valid HTML attributes of the <table> element (e.g. Width , Height , CellSpacing , etc). It also

How to Unit Test HtmlHelper similar to “using(Html.BeginForm()){ }”

£可爱£侵袭症+ 提交于 2019-12-05 17:52:18
Can somebody please suggest how I could write a Unit Test with Moq for following HtmlHelper method? public static HtmlTagBase GenerateTag<T>(this HtmlHelper htmlHelper , object elementData , object attributes) where T : HtmlTagBase { return (T)Activator.CreateInstance(typeof(T) , htmlHelper.ViewContext , elementData , attributes); } which you would use as follows (please note the using statement - this is causing me confusion): <%--Model is a type of ShareClass--%> <% using (Html.GenerateTag<DivTag>(Model)) { %> My Div <% } %> using this method, if you specify T as type DivTag , where

How do you create an HtmlHelper outside of a view in ASP.NET MVC 2.0?

你离开我真会死。 提交于 2019-12-05 14:54:18
We are in the process of upgrading an ASP.NET MVC 1.0 application to the 2.0 release and some of the code requires the use of LinkExtensions which require an HtmlHelper to render. While we know that some of the code doesn't follow the MVC model correctly and are in the process of recoding as needed, we need something to work so get the application to build. Here is the current syntax that we have that works under ASP.NET MVC 1.0: public static HtmlHelper GetHtmlHelper(ControllerContext context) { return new HtmlHelper(new ViewContext(context, new WebFormView("HtmlHelperView"), new

Cannot convert type 'System.Collections.Generic.List<string>' to 'System.Web.Mvc.SelectList'

送分小仙女□ 提交于 2019-12-05 11:45:24
问题 I have the following Action method, which have a viewBag with a list of strings:- public ActionResult Login(string returnUrl) { List<string> domains = new List<string>(); domains.Add("DomainA"); ViewBag.ReturnUrl = returnUrl; ViewBag.Domains = domains; return View(); } and on the view i am trying to build a drop-down list that shows the viewBag strings as follow:- @Html.DropDownList("domains",(SelectList)ViewBag.domains ) But i got the following error :- Cannot convert type 'System

Override EditorForModel Template

元气小坏坏 提交于 2019-12-05 08:52:11
You can provide alternate templates for individual types, but is it possible to override the template that wraps the label, field and validation up. Change: <div class="editor-label"><label for="Content">Content</label></div> <div class="editor-field"><input class="text-box single-line" id="Content" name="Content" type="text" value="" /> </div> To: <div class="field"> <label for="Content">Content</label> <input class="text-box single-line" id="Content" name="Content" type="text" value="" /> </div> Rich You can write an Object.ascx template and perform your own logic. 来源: https://stackoverflow

How to read name of html helper element

做~自己de王妃 提交于 2019-12-05 07:09:38
Is there in mvc any opportunity to read a name which will be assigned to html control? For example I use this code: <div> @Html.LabelFor(x => x.Name) @Html.TextBoxFor(x => x.Name) @Html.ValidationMessageFor(x => x.Name) @Html.HiddenFor(x => x.Id) <div> I want to display here a TextBox name </div> </div> And I want to get a name of input name. This code is fragment of partial view. Name of element looks like children[1].Name archil @Html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName("Name") Or you can use extension method for generic HtmlHelper to use this with Lambda Expressions

Html helper for boolean values in asp.net mvc

拜拜、爱过 提交于 2019-12-05 06:28:43
问题 Are there any html helper methods for displaying boolean values in a dropdown? 回答1: Why not use Html.CheckBox()? 回答2: This is an old thread but still at the top of some searches. You could do this by using the built in DropDownListFor HTML Helper: <%= Html.DropDownListFor(model => Model.MyBooleanProperty,new List<SelectListItem>(){ new SelectListItem(){ Text = "Yes", Value="True"}, new SelectListItem(){ Text = "No", Value="False"}})%> You can also implement your own HTML Helper: public static

Get value from ASP.NET MVC Lambda Expression

本秂侑毒 提交于 2019-12-05 02:58:48
I am trying to create my own HTML Helper which takes in an expression (similar to the built-in LabelFor<> helper. I have found examples to obtain the value of a property when the expression is similar to this: model => model.Forename However, in some of my models, I want to obtain properties in child elements, e.g. model => mode.Person.Forename In these examples, I cannot find anyway to (easily) obtain the value of Forename. Can anybody advise on how I should be getting this value. Thanks If you are using the same pattern that the LabelFor<> method uses, then the expression will always be a

Why don't my HtmlHelper extensions work?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 00:42:24
I'm building an ASP.Net MVC website. Rather than have everything in one project, I've decided to separate the Web, Model and Controller out into different projects in the same solution, that reference each-other. The referencing goes like this: Web ---[references]---> Controller ---[references]---> Model Now I wanted to add 2 custom methods to the HtmlHelper class - they're called "IncludeScript" and "IncludeStyle". They each take a single string parameter, and generate a script or link tag respectively. I've created an extender class, according to documentation on the web, and written the two

Using Html.ActionLink and Url.Action(…) from inside Controller

笑着哭i 提交于 2019-12-05 00:06:53
I want to write an HtmlHelper to render an ActionLink with pre-set values, eg. <%=Html.PageLink("Page 1", "page-slug");%> where PageLink is a function that calls ActionLink with a known Action and Controller, eg. "Index" and "Page". Since HtmlHelper and UrlHelper do not exist inside a Controller or class, how do I get the relative URL to an action from inside a class? Update: Given the additional three years of accrued experience I have now, here's my advice: just use Html.ActionLink("My Link", new { controller = "Page", slug = "page-slug" }) or better yet, <a href="@Url.Action("ViewPage", new