html-helper

What is the easiest way to get the property value from a passed lambda expression in an extension method for HtmlHelper?

故事扮演 提交于 2019-11-30 10:20:08
问题 I am writing a dirty little extension method for HtmlHelper so that I can say something like HtmlHelper.WysiwygFor(lambda) and display the CKEditor. I have this working currently but it seems a bit more cumbersome than I would prefer. I am hoping that there is a more straight forward way of doing this. Here is what I have so far. public static MvcHtmlString WysiwygFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression) { return MvcHtmlString

How to set the default value for Html.DropDownListFor in MVC

房东的猫 提交于 2019-11-30 09:57:00
问题 i have following code : controller method : public ActionResult Register(int? registrationTypeId) { IEnumerable<AccountType> accountTypes = new List<AccountType> { new AccountType { AccountTypeId = 1, AccountTypeName = "Red" }, new AccountType { AccountTypeId = 2, AccountTypeName = "Blue" } }; // I want to select account type on registrationTypeId ViewBag.AccountTypes = accountTypes; return View(); } View <div class="col-md-10"> @Html.DropDownListFor(n => n.AccountType, new SelectList(ViewBag

Html.CheckBox returns false if disabled, even if seleced

我的未来我决定 提交于 2019-11-30 08:10:31
Hopefully an easy question for you asp.net mvc gurus: I have a checkbox, created like this: <%=Html.CheckBox("MyCheckBox", true, new { disabled = "disabled"})%> In my action I am checking the value like so: bool isChecked = form["MyCheckBox"].Contains("true"); I expect this to return true, as it is checked. However the hidden element that gets created has a false value: <input checked="checked" disabled="disabled" id="MyCheckBox" name="MyCheckBox" type="checkbox" value="true" /> <input name="MyCheckBox" type="hidden" value="false" /> First, is there a way to make the HtmlHelper behave as I

@Html.DisplayNameFor for details model

℡╲_俬逩灬. 提交于 2019-11-30 08:05:26
In my model I have an Entity public class Carrier { public Guid CarrierId { get; set; } public string Name { get; set; } } I also have a ViewModel public class CarrierIndexViewModel { public IEnumerable<Carrier> Carriers { get; set; } public PagingInfo PagingInfo { get; set; } } I have a strongly-typed (CarrierIndexViewModel) Index View, which is suppose to display a table of Carrier using PagingInfo. I'm trying to use Html helper to display Carrier.Name in my table's header When I used IEnumerable as a model for this view I had @Html.DisplayFor(model => model.Name) How can I get same result

Html.TextBox conditional attribute with ASP.NET MVC Preview 5

旧城冷巷雨未停 提交于 2019-11-30 07:56:48
I have a strongly-typed MVC View Control which is responsible for the UI where users can create and edit Client items. I'd like them to be able to define the ClientId on creation, but not edit, and this to be reflected in the UI. To this end, I have the following line: <%= Html.TextBox("Client.ClientId", ViewData.Model.ClientId, new { @readonly = (ViewData.Model.ClientId != null && ViewData.Model.ClientId.Length > 0 ? "readonly" : "false") } ) %> It seems that no matter what value I give the readonly attribute (even "false" and ""), Firefox and IE7 make the input read-only, which is annoyingly

Radio Button generates duplicate HTML id-s

依然范特西╮ 提交于 2019-11-30 07:52:34
It seems that the default ASP.NET MVC2 Html helper generates duplicate HTML IDs when using code like this (EditorTemplates/UserType.ascx): <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<UserType>" %> <%: Html.RadioButton("", UserType.Primary, Model == UserType.Primary) %> <%: Html.RadioButton("", UserType.Standard, Model == UserType.Standard) %> <%: Html.RadioButton("", UserType.ReadOnly, Model == UserType.ReadOnly) %> The HTML it produces is: <input checked="checked" id="UserType" name="UserType" type="radio" value="Primary" /> <input id="UserType" name="UserType" type=

How to use MVC Html Helper .DropDownListFor<> with an Enum

我与影子孤独终老i 提交于 2019-11-30 07:32:49
In my MVC 3 Razor app, I have a Model with an enum.. Model Example: public class EmployeeModel { public enum Title { Accountant = 111, Sales = 222, Production = 333 } [Required] public string Name {get; set;} [Required] public Title JobTitle {get; set;} } In my View I would like to use the Html helpers to build an Html Form... View Example: @model ..Models.EmployeeModel @using (Html.BeginForm()) { @Html.LabelFor(m => m.Name) @Html.TextBoxFor(m => m.Name) <br> @Html.LabelFor(m => m.JobTitle) @Html.DropDownListFor(m => m.JobTitle, ??How do I get Title enum values??) <br> <input type="submit /> }

Adding a custom HTML Helper to MVC Project

怎甘沉沦 提交于 2019-11-30 07:30:39
I've been browsing the web trying to find a good example/tutorial detailing how I can create and use my own custom HTML Helpers for my MVC 3 Razor application I found this one which is as follows Adding your own HtmlHelper in ASP.NET MVC 3 I've created a class (trimmed it down a bit) as so using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Linq.Expressions; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Html; namespace MyWebApp { public static class ExtensionMethods { public static MvcHtmlString StateDropDownListFor<TModel,

Foreach on IEnumerable property and CheckBoxFor in ASP.Net MVC

感情迁移 提交于 2019-11-30 06:56:42
I believe this question applies to any of the "For" Html helpers, but my specific problem is using CheckBoxFor... I have a model that is of type IEnumerable, where rights is a simple POCO. This model is actually a property of a bigger model that I created an EditorTemplate for. Here is the bigger picture of my model: public class bigmodel { public string Title {get; set;} public string Description {get; set;} [UIHint("ListRights")] public IEnumerable<rights> Rights {get;set;} } public class rights { public bool HasAccess {get; set;} public string Description {get;set;} } I created an

Extract Display name and description Attribute from within a HTML helper

一世执手 提交于 2019-11-30 04:01:04
I am building a custom HTML.LabelFor helper that looks like this : public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> self, Expression<Func<TModel, TValue>> expression, Boolean showToolTip) { var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData); ... } To be able to get the proper name for the property I am using the following code : metadata.DisplayName And on the property of the ModelView class I got : [DisplayName("Titel")] The problem is that I also need a description. There is an Attribute called Display and that has Name and Description