html-helper

What does 'this' keyword mean in a method parameter? [duplicate]

强颜欢笑 提交于 2019-11-27 03:10:40
问题 This question already has an answer here: What are Extension Methods? 11 answers namespace System.Web.Mvc.Html { // Summary: // Represents support for HTML in an application. public static class FormExtensions { public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName); ... } } I have noticed that 'this' object in front of the first parameter in BeginForm method doesn't seem to be accepted as a parameter. Looks like in real BeginForm methods

What's the difference between RouteLink and ActionLink in ASP.NET MVC?

≡放荡痞女 提交于 2019-11-27 03:07:24
I think that the title pretty much sums it up: What's the difference between RouteLink() and ActionLink() in ASP.NET MVC? i.e. when do you use Html.RouteLink() and when do you use Html.ActionLink() in your View? Action and Routes don't have to have a 1:1 relationship. ActionLink will generate the URL to get to an action using the first matching route by action name. RouteLink will generate a URL to a specific route determined either by name or route values. Actually, the output from the two methods is the same, but it is generated in slightly different ways: Html.ActionLink() makes it easy to

Get DisplayName Attribute without using LabelFor Helper in asp.net MVC

那年仲夏 提交于 2019-11-27 03:02:44
What is the best way to retrieve the display name attribute for an item in your model? I see a lot of people using the LabelFor helper for everything, but a label isn't appropriate if I just want to list the data out. Is there an easy way just get the Name Attribute if I just want to print it out in, say a paragraph? <p> <%= Html.Encode( ModelMetadata.FromLambdaExpression<YourViewModel, string>( x => x.SomeProperty, ViewData).DisplayName ) %> <p> Obviously in order to avoid the spaghetti code it is always a good idea to write a helper: public static class HtmlExtensions { public static

Unit testing HtmlHelper extension method fails

妖精的绣舞 提交于 2019-11-27 02:55:48
问题 I am trying to test some HtmlHelper extension methods I have written. My first problem was how to create a HtmlHelper instance, but I solved that using this code: private static HtmlHelper<T> CreateHtmlHelper<T>(T model) { var viewDataDictionary = new ViewDataDictionary(model); var controllerContext = new ControllerContext(new Mock<HttpContextBase>().Object, new RouteData(), new Mock<ControllerBase>().Object); var viewContext = new ViewContext(controllerContext, new Mock<IView>().Object,

Raw ActionLink linkText

我的梦境 提交于 2019-11-27 02:20:54
问题 I want to put a button as the text of an @ActionLink() but I can't because it HTML-escapes my string... I found the @Html.Raw() mechanism and have tried the @ActionLink().ToHtmlString() but can't figure out how to put it together... I found an article that describes building an extension for a similar purpose but it's eeky to go to that much trouble... there must be an easy way? 回答1: You could write a helper: public static class HtmlExtensions { public static IHtmlString MyActionLink( this

Why not use Html.EditorForModel()

血红的双手。 提交于 2019-11-27 01:30:00
问题 Ok I just discovered about the EditorForModel in MVC and I want to know when I should use this instead of an EditorFor on each of my property? And why does when I add a strongly typed view it does not use this and build an EditorFor on every property? I'm late on this... but thanks for the info! 回答1: Since the accepted answer is a link-only answer (and was removed), I thought I'd actually answer the question derived from Brad Wilson's Blog: ASP.NET MVC 2 Templates, Part 1: Introduction. The

Testing HtmlHelpers in ASP.NET MVC

烈酒焚心 提交于 2019-11-27 00:37:18
问题 Is there any way to (unit) test my own HtmlHelpers? In case when I'd like to have custom control (rendered by HtmlHelper) and I know requierements for that control how could I write tests first - and then write code? Is there a specific (nice) way to do that? Is it worth? 回答1: The main problem is that you have to mock the HtmlHelper because you may be using methods of the helper to get routes or values or returning the result of another extension method. The HtmlHelper class has quite a lot

ASP.NET MVC: Hidden field value does not get rendered using HtmlHelper.Hidden

本小妞迷上赌 提交于 2019-11-27 00:01:54
问题 Something pretty weird is happening with my app: I have the following property in my ViewModel: public int? StakeholderId { get; set; } It gets rendered in a partial view as follows: <%= Html.Hidden("StakeholderId", Model.StakeholderId) %> The form is submitted, and the relevant controller action generates an id and updates the model, before returning the same view with the updated model The problem I'm experiencing is that the hidden field does not have anything in its "value" attribute

How can I add a class attribute to an HTML element generated by MVC's HTML Helpers?

霸气de小男生 提交于 2019-11-26 23:53:19
问题 ASP.NET MVC can generate HTML elements using HTML Helpers, for example @Html.ActionLink() , @Html.BeginForm() and so on. I know I can specify form attributes by creating an anonymous object and pass that object for the (fourth in this case) htmlAttributes parameter where specifying an id for the element: Html.BeginForm("Foo", "Bar", FormMethod.Post, new { id = "MyForm"}) But what about the class attribute? Obviously this does not work: Html.BeginForm("Foo", "Bar", FormMethod.Post, new { class

Generate URL in HTML helper

走远了吗. 提交于 2019-11-26 23:52:30
问题 Normally in an ASP.NET view one could use the following function to obtain a URL (not an <a> ): Url.Action("Action", "Controller"); However, I cannot find how to do it from a custom HTML helper. I have public class MyCustomHelper { public static string ExtensionMethod(this HtmlHelper helper) { } } The helper variable has the Action and GenerateLink methods, but they generate <a> ’s. I did some digging in the ASP.NET MVC source code, but I could not find a straightforward way. The problem is