html-helper

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

怎甘沉沦 提交于 2019-12-10 02:02:21
问题 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

Why don't my HtmlHelper extensions work?

£可爱£侵袭症+ 提交于 2019-12-10 01:43:15
问题 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

HtmlHelper extension method vs partial view?

倾然丶 夕夏残阳落幕 提交于 2019-12-10 00:44:49
问题 I curious when it's recommended to use HtmlHelper extension method and when to use partial view? They seem to be equally eligible in many cases. 回答1: I personally think that partial view is more or less similar to usercontrol in asp.net, which act as a group of reusable functional elements. For example, if you need to make a login control box that may appear across the whole site, partial view would be better. What's more for partial view is, it is actually self-contained with its own

How do I generate a URL outside of a controller in ASP.NET MVC?

房东的猫 提交于 2019-12-09 08:01:50
问题 How do I generate a URL pointing to a controller action from a helper method outside of the controller? 回答1: Pass UrlHelper to your helper function and then you could do the following: public SomeReturnType MyHelper(UrlHelper url, // your other parameters) { // Your other code var myUrl = url.Action("action", "controller"); // code that consumes your url } 回答2: You could use the following if you have access to the HttpContext : var urlHelper = new UrlHelper(HttpContext.Current.Request

Show “NULL” for null values in ASP.NET MVC DisplayFor Html Helper

倾然丶 夕夏残阳落幕 提交于 2019-12-09 07:48:37
问题 Is there a way to get an @Html.DisplayFor value to show "NULL" in the view if the value of the model item is null ? Here's an example of an item in my Details view that I'm working on currently. Right now if displays nothing if the value of the Description is null . <div class="display-field"> @Html.DisplayFor(model => model.Description) </div> 回答1: yes, I would recommend using the following data annotation with a nullable datetime field in your codefirst model : [Display(Name = "Last

Html.BeginForm() type of extension

坚强是说给别人听的谎言 提交于 2019-12-09 06:11:15
问题 Does anyone know the syntax for creating a custom HtmlHelperextension method which behaves like.. <% using (Html.BeginForm()) {%> <p>Loads of html stuff here </p> <% } %> I'm thinking of something along the lines of.... Any ideas? Cheers, ETFairfax 回答1: You need to create a class that implements IDisposable interface and return that from your HtmlHelper . public static class HtmlHelperTableExtensions { private class TableRenderer : IDisposable { HtmlHelper html; public TableRenderer

Html.TextAreaFor in asp.net mvc

只愿长相守 提交于 2019-12-09 02:29:34
问题 I have a asp.net mvc view that allows the user to enter some description in a textarea. There are two problems that I am facing. Either when I expand the textarea it is expanding without moving other html elements or I am not able to make create a Html.TextBoxFor () multiline textbox. Can anyone suggest a solution to this? If Use Textarea how to make it expand(grow in size) so that it does not overlap with other elements or how to use Html.TextBoxFor() for multiline? This is how my code looks

MVC3 HTML helper doesn't update DropdownListFor on Submit the form

北城余情 提交于 2019-12-08 19:54:26
I have a simple HTML form with dropdwonListFor bound to colors, a textBox below it and submit button to submit the form and save the color. When I select a color from the dropdownlist, it will change the value of the textbox below it, if the user clicks the submit form. it goes back to the controller and I save the color from the texebox and return view(model) as an action result, but the problem that the dropdownlistfor doesn't get updated with the value of the textbox whether the value in the textbox within the dropdownlist or not. By the way you can test it urself Can anybody help please ?

can request querystring be accessed from htmlhelper

天涯浪子 提交于 2019-12-08 14:29:53
问题 Hi Can query string be accessed in HTMLHelper extension methods. We need to render differently depending on the querystring in the request. 回答1: Yes, through the current context, which is a property on HTML Helper. public static string DoThis(this HtmlHelper helper) { string qs = helper.ViewContext.HttpContext.Request.QueryString.Get("val"); //do something on it } 回答2: Sure: public static MvcHtmlString Foo(this HtmlHelper htmlHelper) { var value = htmlHelper.ViewContext.HttpContext.Request[

How can I invoke `EditorExtensions.EditorFor` in my HtmlHelper?

北战南征 提交于 2019-12-08 12:41:41
问题 I use different Models in my CreateView, all inherit from BaseModel. To call the right EditorFor I have created a HtmlHelper that gets the Model and the actual property. But I don´t know how to invoke it. BaseModel: public abstract class BaseModel { protected IEnumerable<PropertyInfo> PropertyInfoCache { get; set; } protected IEnumerable<EnumeratedProperty> EnumeratedPropertyCache { get; set; } protected BaseModel() { PropertyInfoCache = this.GetType().GetProperties(); EnumeratedPropertyCache