html-helper

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

佐手、 提交于 2019-12-31 00:45:16
问题 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? 回答1: Please see the following posts, this

HTML.Checkboxfor to Toggle button HTML?

人走茶凉 提交于 2019-12-30 08:37:21
问题 Is there anyway to convert the @HTML.Checkboxfor into toggle html button? I have refer the toggle button style from https://www.w3schools.com/howto/howto_css_switch.asp Currently its working by using : @Html.CheckBoxFor(model => model.IsEnabled, new { @checked = "checked", @Name = "DelegateChkBox" }) Changed to: <td class="slider-td"> <label class="switch"> <input name="DelegateChkBox" id="IsEnabled" type="checkbox" value="true" data-val-required="The IsEnabled field is required." data-val=

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

喜你入骨 提交于 2019-12-30 06:54:09
问题 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). 回答1: 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,

Create using for own helper? like Html.BeginForm

此生再无相见时 提交于 2019-12-29 04:45:13
问题 I was wondering, is it possible to create your own helper definition, with a using? such as the following which creates a form: using (Html.BeginForm(params)) { } I'd like to make my own helper like that. So a simple example I'd like to do using(Tablehelper.Begintable(id) { <th>content etc<th> } which will output in my view <table> <th>content etc<th> </table> Is this possible? if so, how? Thanks 回答1: Sure, it's possible: public static class HtmlExtensions { private class Table : IDisposable

SelectListItem with data-attributes

落爺英雄遲暮 提交于 2019-12-28 01:51:28
问题 Is there anyway to have a SelectList prepopulated on ViewModel with data-attributes ? I want to do @Html.DropdownListFor(m=> m.CityId, Model.Cities); so it generates code like : <select id="City" class="location_city_input" name="City"> <option data-geo-lat="-32.522779" data-geo-lng="-55.765835" data-geo-zoom="6" /> <option data-geo-lat="-34.883611" data-geo-lng="-56.181944" data-geo-zoom="13" data-geo-name="Montevideo" data-child=".state1" value="1">Montevideo</option> <option data-geo-lat="

What does Html.HiddenFor do?

馋奶兔 提交于 2019-12-27 13:58:46
问题 Although I have read the documentation on Html.HiddenFor, I've not grasped what is it used for... Could somebody explain its uses and give a short example? Where should those helpers go in the code? 回答1: It creates a hidden input on the form for the field (from your model) that you pass it. It is useful for fields in your Model/ViewModel that you need to persist on the page and have passed back when another call is made but shouldn't be seen by the user. Consider the following ViewModel class

Jquery Field Validation onblur (mvc)

自作多情 提交于 2019-12-26 03:46:25
问题 I want to have the fields in my form be validated after the user clicks out of them. However, I am not familiar with client side validation using Jquery. Can anyone explain how to validate that the field is not empty? <label id="lblAccountName">Account Name</label> @Html.TextBoxFor(model => model.Pharmacy.AccountName, new { @id = "txtAccountName", @Name = "txtAccountName" }) 回答1: This can be achieved with JS var accName = document.getElementById('txtAccountName'); accName.addEventListener(

Problem binding selected value to DropDownListFor inside Editor Template

允我心安 提交于 2019-12-25 08:26:03
问题 Description I have a payment page that includes a form for entering bank account information. I have encapsulated the bank account information into a Model / Editor Template. The page itself has its own View Model, which happens to contain a BankAccount property to be passed in to the Editor. [[View Models]] public class PaymentPageModel { public SomeProperty1 { get; set; } public SomeProperty2 { get; set; } public BankAccount BankAccount { get; set; } ... } public class BankAccount { public

MVC 3 - HTML Helper

二次信任 提交于 2019-12-24 23:50:32
问题 I was going to use declarative HTML helpers, but then found out that they have not been implemented in a release of MVC 3. I'm trying to get old HTML helpers to work with the following code: private static String GenerateSingleOptionHTML(Question q) { String ret = ""; for(int i = 0; i < 3; i++) { ret += String.Format("<li><input type=\"radio\" id=\"Q" + i +"\" value=\"" + i + "\" name=\"Q" + i +"\" />" + q.Body + "</li>"); } return ret; } Ignore the html and tag as they work fine. What I get

Strange problem with ASP.NET MVC DropDownFor

余生长醉 提交于 2019-12-24 20:23:57
问题 Hi, I have the following in my view : <form id="list_ad" method="get" class="adListFilter" action="<%=Url.Action("List", "Ad") %>"> <%: Html.DropDownListFor(model => model.LS.L1, Model.LS.Location1List, "-- Place --", new { @class = "dd1" })%> ... </form> model.LS.L1 is int? Model.LS.Location1List is SelectList (no selection set only the list) First visit on the view will look grate, the LocationDropDown will contain correct values and the model.LS.L1 will be set to null. Then I submit the