html-helper

Controlling Output Indentation in ASP.Net MVC

孤者浪人 提交于 2019-12-02 02:23:05
My colleague is extremely 'hot' on properly formatted and indented html being delivered to the client browser. This is so that the page source is easily readable by a human. Firstly, if I have a partial view that is used in a number of different areas in my site, should the rendering engine be automatically formatting the indentations for me (ala setting the Formatting property on an XmlTextWriter)? Secondly, my colleague has created a number of HtmlHelper extension methods for writing to the response. These all require a CurrentIndent parameter to be passed to them. This smells wrong to me.

MVC3 HTMLHelper defaults

廉价感情. 提交于 2019-12-02 02:08:43
I have an html helper that I'd like to set a default on. @Html.EditorFor(model => model.DateFrom) What's the syntax to set the default value of the helper if the model.DateFrom is null? I dont think that using EditorFor you can set a default value. Consider setting it in the accessors on your model? To do it on other types ( TextBoxFor etc ) You can set a value but not a default value. So you would need to do: @if(Model.something == null) { @Html.TextBoxFor(m => m.ID, new { @Value = "Value!"}) } else { @Html.TextBoxFor(m => m.ID) } As I would reccomend: private DateTime? _date; public DateTime

C# html-helper extend existing method without overriding?

北城以北 提交于 2019-12-02 01:35:14
I have already searched for the question and found possible answer , but I still need some help. I am trying to write an html-helper to extend functionality of already existing LabelFor method public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData); string htmlFieldName = ExpressionHelper.GetExpressionText(expression); //string labelText = metadata.DisplayName ?? metadata.PropertyName ??

Adapting a Custom Html Helper for Razor (it uses HtmlTextWriter so returns void)

╄→гoц情女王★ 提交于 2019-12-01 22:07:49
The Problem I have a very nifty menu Html helper written for WebFormViewEngine views. This engine allows your helpers to return void, and still be able to use: @Html.Theseus This is great for my helper, because it can then render the menu using HtmlTextWriter, that renders directly to the output stream. In Razor views, however, the Html helpers are expected to return a value (usually MvcHtmlString) which is what gets added to the output. Small difference, big consequence. There is a way around this, as pointed out to me by GvS (see ASP.NET MVC 2 to MVC 3: Custom Html Helpers in Razor ) as

Filter a model's attributes before outputting as json

元气小坏坏 提交于 2019-12-01 20:45:38
Hey guys, I need to output my model as json and everything is going fine. However, some of the attributes need to be 'beautified' by filtering them through some helper methods, such as number_to_human_size . How would I go about doing this? In other words, say that I have an attribute named bytes and I want to pass it through number_to_human_size and have that result be output to json. I would also like to 'trim' what gets output as json if that's possible, since I only need some of the attributes. Is this possible? Can someone please give me an example? I would really appreciate it.

How to add custom data attributes and classes to `@Html.EditorFor`?

时间秒杀一切 提交于 2019-12-01 20:12:00
I want to add some custom attributes to the input generated by @Html.EditorFor , I tried the following: @Html.EditorFor(model => model.Percent, new { @class = "percent" }) But it just ignores my class, from what I can tell from searching around is that the template doesn't support adding custom attributes. But how does one create a custom template adding support for the custom attributes, while keeping all the functionality of the old template? Brendan Vogt Please see the following posts, this question has been asked before on Stackoverflow. Add css class to Html.EditorFor in MVC 2 Set the

How to determine if a PropertyType is foreign key

血红的双手。 提交于 2019-12-01 18:14:44
I have the following class 'schakeling', generated with EF, representing a database table 'schakeling'. In the database 'id' is the primary key and 'plc_id' is a foreign key. public partial class schakeling { public schakeling() { this.verbruik = new HashSet<verbruik>(); } public int id { get; set; } public int plc_id { get; set; } public string var_output { get; set; } public string omschrijving { get; set; } public int vermogen { get; set; } public Nullable<bool> status { get; set; } public Nullable<byte> hourOn { get; set; } public Nullable<byte> minuteOn { get; set; } public Nullable<byte>

Mvc UserControl using HtmlHelper with HelperResult and MvcHtmlString

人走茶凉 提交于 2019-12-01 18:14:29
问题 I'm developing an application in asp mvc, and I want to create some friendly controls for developers, like Server controls in asp.net WebForms . In this case, I want to create a bootstrap panel that has some fixed html and render inside a template of items. I am doing it this way: public static MvcHtmlString BootPanel<T>(this IEnumerable<T> items, Func<T, HelperResult> template, string title = "") { StringBuilder sb = new StringBuilder(); sb.Append("<div class='panel panel-info'>"); sb.Append

Mvc UserControl using HtmlHelper with HelperResult and MvcHtmlString

别说谁变了你拦得住时间么 提交于 2019-12-01 17:56:54
I'm developing an application in asp mvc, and I want to create some friendly controls for developers, like Server controls in asp.net WebForms . In this case, I want to create a bootstrap panel that has some fixed html and render inside a template of items. I am doing it this way: public static MvcHtmlString BootPanel<T>(this IEnumerable<T> items, Func<T, HelperResult> template, string title = "") { StringBuilder sb = new StringBuilder(); sb.Append("<div class='panel panel-info'>"); sb.Append("<div class='panel-heading'>"); sb.Append(title); sb.Append("</div>"); sb.Append("<div class='panel

MVC View: Type arguments Html helper DisplayFor cannot be inferred from the usage

主宰稳场 提交于 2019-12-01 17:54:12
I'm trying to make use of the extended HTML Helper DisplayFor in this View: <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcCms.Web.ViewModels.SubscriptionsViewModel>" %> <% using (Html.BeginForm("TrainingSubscription", "Account", FormMethod.Post)) { %> <%: Html.DisplayFor(m => m.Subscriptions) %> <input type="submit" value="Save" /> <% } %> with the following ViewModel namespace MvcCms.Web.ViewModels { public class SubscriptionsViewModel { public string TrainingId { get; set; } public string Subject { get; set; } public IEnumerable