html-helper

How to create Expression<Func<TModel, TProperty>>;

∥☆過路亽.° 提交于 2019-11-30 21:47:31
Is it possible to create Expression<Func<TModel, bool>>() which can be used in different htmlHelpers (for instance in CheckBoxFor() ), if I have a model object this HtmlHelper<TModel> htmlHelper and name of the property (through reflection). Sure: static Expression<Func<TModel,TProperty>> CreateExpression<TModel,TProperty>( string propertyName) { var param = Expression.Parameter(typeof(TModel), "x"); return Expression.Lambda<Func<TModel, TProperty>>( Expression.PropertyOrField(param, propertyName), param); } then: var lambda = CreateExpression<SomeModel, bool>("IsAlive"); 来源: https:/

Dynamically call textboxfor with reflection

依然范特西╮ 提交于 2019-11-30 20:29:16
问题 The end result of what I am trying to do is build a form dynamically by reflecting an object and it's properties. I've created HtmlHelper methods that call TextBoxFor and CheckBoxFor and so on, but now I need help figuring out how to properly reflect the property when passing it to Html.TextBoxFor Here's the helper method: public static MvcHtmlString FormTextBox<TModel>(this HtmlHelper<TModel> helper, String id, String property_name, object model, RouteValueDictionary attributes) { Type model

MVC3 Html.BeginForm - passing arguments as RouteValueDictionary fails

…衆ロ難τιáo~ 提交于 2019-11-30 20:06:55
I have a multi-step setup process where I would like to pass query string arguments appended to the URL only if they are relevant. http://localhost:6618/Account/Profile?wizard=true&cats=dogs @using( Html.BeginForm() ) worked great. It yielded: <form action="/Account/Profile?setup=true&cats=dogs" method="post"> i.e. it passed into the POST action any of the original query string parameters, and then in that Controller action I could chose which ones were relevant to pass to my next step, or necessary to add, by adding to the RouteValues and a RedirectToResult. However, I need to assign a class

ASP.NET MVC Beta 1 - where is Html.RenderPartial?

时间秒杀一切 提交于 2019-11-30 18:23:44
I'm just in the process of upgrading my Preview 5 application to Beta 1, and I'm nearly there save for this one error when trying to render a control: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'RenderPartial' and no extension method 'RenderPartial' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?) My markup (in the .aspx View Content Page) is: <% Html.RenderPartial("Controls/UserForm", ViewData); %> I've tried using Microsoft.Web.Mvc but to no avail. Does anyone know where Html

Custom HtmlHelper extension method not available in View?

 ̄綄美尐妖づ 提交于 2019-11-30 17:51:50
问题 I have translated Jeremiah Clark's CheckBoxList Helper for MVC into my VB.Net project but when I try to use the method in my view I get the error 'CheckBoxList' is not a member of 'System.Web.Mvc.HtmlHelper(Of Attenda.Stargate.Web.UserRolesViewModel)'. Can anyone tell me where I have gone wrong? Helper module: Imports System.Runtime.CompilerServices Public Module InputExtensions <Extension()> _ Public Function CheckBoxList(ByVal htmlHelper As HtmlHelper, ByVal name As String, ByVal listInfo

How to resolve issue with image path when testing HtmlHelper?

不想你离开。 提交于 2019-11-30 13:48:54
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 known." var imgPath = VirtualPathUtility.ToAbsolute("~/Images/SortingArrowUp.png"); How can I solve this. I

Html.CheckBox returns false if disabled, even if seleced

筅森魡賤 提交于 2019-11-30 13:39:46
问题 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=

How to define form field prefix in ASP.NET MVC

点点圈 提交于 2019-11-30 11:33:51
I am attempting to render a composite ProductCatalog view in ASP.NET MVC. This requires that I render multiple Product views per page. Each product view is a separate form. I need the form fields to have a prefix based on the id, so that I don't have duplicate IDs in the rendered document. Is there a way to define a prefix to be applied to all the form fields that are generated by the Html Extensions, or do I need to build this out manually? Yes, you can define a prefix for the controls inside your view based on the executing action, consider the following code that should be place in your GET

Custom html helpers in MVC 4

痴心易碎 提交于 2019-11-30 11:22:55
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 Darin Dimitrov and added namespace to webconfig <add namespace="SEM.API.Helpers" /> Make sure you did this in ~/Views/web.config and not in ~/web.config . Another thing to try is to add the @using directive to your view: @using SEM.API.Helpers @Html

@Html.DropDownListFor How to add option?

三世轮回 提交于 2019-11-30 11:02:18
@Html.DropDownListFor(model => model.ZipFile, new SelectList(ViewBag.ZipFiles)) The above code creates me a select list just fine. But I want to make the selection optional. Unfortunately there is no empty option and I'd like to add one in. How would I do this? By using the proper DropDownListFor overload : @Html.DropDownListFor( model => model.ZipFile, new SelectList(ViewBag.ZipFiles), "-- please select a zip file --" ) user5093080 @Html.DropDownListFor(model => model.Country, new List<SelectListItem> { new SelectListItem { Text = "India", Value = "1"}, new SelectListItem { Text = "USA",