html-helper

Exclude/Remove Value from MVC 5.1 EnumDropDownListFor

大城市里の小女人 提交于 2019-11-29 23:18:40
I have a list of enums that I am using for a user management page. I'm using the new HtmlHelper in MVC 5.1 that allows me to create a dropdown list for Enum values. I now have a need to remove the Pending value from the list, this value will only ever be set programatically and should never be set by the user. Enum: public enum UserStatus { Pending = 0, Limited = 1, Active = 2 } View: @Html.EnumDropDownListFor(model => model.Status) Is there anyway, either overriding the current control, or writing a custom HtmlHelper that would allow me to specify an enum, or enums to exclude from the

URL.Action() including route values

浪尽此生 提交于 2019-11-29 22:46:57
I have an ASP.Net MVC 4 app and am using the Url.Action helper like this: @Url.Action("Information", "Admin") This page is used for both adding a new and edit an admin profile. The URLs are as follows: Adding a new: http://localhost:4935/Admin/Information Editing Existing: http://localhost:4935/Admin/Information/5 <==Admin ID When I'm in the Editing Existing section of the site and decide that I would like to add a new admin I click on the following link: <a href="@Url.Action("Information", "Admin")">Add an Admin</a> The problem however that the above link is actually going to http://localhost

Accessing attributes from Custom Html Helpers in asp.net mvc 4 razor

与世无争的帅哥 提交于 2019-11-29 20:28:29
I have created HtmlHelper in ASP.NET MVC 4 razor view engine C#. Can I pass view model property to my helper class? For example, I have property [Required] [Display(Name = "Your Lastname")] public string Lastname { get; set; } Can I pass this property to my helper something like this @Html.Example(model => model.Lastname) and then get data annotations in helper (if this field is required what is display name and etc.)? The [Display] attribute enriches the metadata. So you could fetch the information from the metadata. For example if you wanted to retrieve the display name inside the helper:

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

喜你入骨 提交于 2019-11-29 19:51:28
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.Create(string.Concat("<textarea class=\"ckeditor\" cols=\"80\" id=\"", expression.MemberName(), "\" name=\

How to resolve issue with image path when testing HtmlHelper?

≡放荡痞女 提交于 2019-11-29 19:00:15
问题 I came across an issue when I was testing my HTML Helper. Basically I'm creating a grid with loads of rows, columns and different types of data in it. In the header there is also a image to notify the user what column the data is sorted by. However, when I'm writing my test now (way too late, but better late than never right?!), I get this error thrown: "The application relative virtual path '~/Images/SortingArrowUp.png' cannot be made absolute, because the path to the application is not

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

耗尽温柔 提交于 2019-11-29 17:57:58
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.AccountTypes, "AccountTypeId", "AccountTypeName"), new { @class = "form-control" }) </div> Model

Custom html helpers in MVC 4

一个人想着一个人 提交于 2019-11-29 17:06:08
问题 I created the helper class namespace SEM.API.Helpers { public static class Navigation { public static string BuildSomething(this HtmlHelper helper) { return "empty"; } } } And added the namespace to webconfig <add namespace="SEM.API.Helpers" /> but I still getting an error: CS1061: "System.Web.Mvc.HtmlHelper" It isn't solved after a lot of rebuilds 回答1: and added namespace to webconfig <add namespace="SEM.API.Helpers" /> Make sure you did this in ~/Views/web.config and not in ~/web.config .

ASP.NET MVC 3 Model Id using Route Id value

久未见 提交于 2019-11-29 14:33:34
Scenario Route: /template/customize/10 Where: 10 = ID of Template() In the controller the model is created based on the template so that the View's model is actually a Customization() object which actually has an Id of 0 because it's new. In the view I render @Html.HiddenFor( m => m.Id ) and the resulting value of that hidden input is 10 , though it should be 0 because m is of type Customization. I've run into this before with MVC 2 and worked around it by not using helper methods. Questions Is there annotation or something I can add to the Html Helper method to actually render the correct

Create a custom ActionLink

巧了我就是萌 提交于 2019-11-29 12:51:22
I want to create a custom actionlink but I don't know how to do this while fulfilling my needs. I worked with custom htmlhelpers before but this is a bit more tricky for me. The actionlink that I want to call needs to be like this: @Html.CustomActionLink("LinkText", "Area","Controller","TabMenu","Action",routeValues, htmlAttributes) so an example would be: @Html.CustomActionLink("Click here","Travel","Trip","Index","Index", new { par1 = "test", par2 = test2, new { @class = "font-color-blue" })` Which would generate this html: <a class="font-color-blue" href="/Trip/Travel/Index/Index?par1=test

How to format the value in a strongy typed view when using ASP.Net MVC's Html.TextBoxFor

流过昼夜 提交于 2019-11-29 11:28:07
问题 I am trying to format a date rendered by ASP.Net MVC's TextBoxFor using the value of a strongly typed view. The date is nullable so if it is null I want to see a blank value, otherwise I want to see it in the format MM/dd/yyyy. <%= Html.TextBoxFor(model => model.BirthDate, new { style = "width: 75px;" })%> Thanks, Paul Speranza 回答1: You can keep the strong typing by using a custom editor template and Html.EditorFor() instead of Html.TextBoxFor() . Create a new EditorTemplates folder in your