I want to use this properties data-number-to-fixed and data-number-stepfactor on my Html.EditorFor control but it does not allow me to do that. Currently the Editor for looks like this
#Html.EditorFor(model => model.CreditCardFees, new { htmlAttributes = new { #class = "form-control text-right", #step = "0.01", #type = "number" } })
But when I add those 2 attributes Visual Studio's Intellisense complains like such
and when I run it will show compiler error
Compiler Error Message: CS1525: Invalid expression term 'fixed'
Any ideas how do I use these 2 attributes?
Looks like I found an answer, just use _ "underscore" in replacement of the - "dashes", MVC will convert it to dashes on compile time.
Related
In my view I have
#model Models.ViewModel.MyViewModel
I can get the values displayed with the Html.DisplayNameFor(model => model.my_id) method. This works fine.
But I cannot get the value of a field in the model by doing anything like
int id = model.my_id;
Every time I use "model," Visual Studio cannot find a reference to it. It does for the Html Helpers, but not when model is not in an Extension method like in the HtmlHelper (I thought it was an Extension Method).
I have tried many variations and various solutions I found on SO. I am missing something fundamental, but I cannot find the answer of how to get the reference to model and its values.
It is also causing this to fail,
#Html.ActionLink("Edit", "Edit", new { id = model.my_id })
What am I doing wrong?
Use Model with a capital M.
#{
int id = Model.my_id;
}
#Html.ActionLink("Edit", "Edit", new { id = Model.my_id })
I tried this one, but not working
#Html.EditorFor(model => model.discount, new { htmlAttributes = new { #class = "form-control", #type = "number", #step = "0.01", #min = "1", #max = "99999" } })
Use maxlength and a TextBoxFor instead of EditorFor
EditorFor has no overload that permit to do that.
This could be even more interesting for you :
maxlength attribute of a text box from the DataAnnotations StringLength in Asp.Net MVC
#minlength="10",#maximumlength="10"
u can use textboxfor or editorfor
anything will work......
I have a #Html.DropDownListFor for my form-control that has multiple countries to choose from in a drop down menu. I need this form-control to be ADA compliant, so I need to have a label associated with it. This is the form-control:
#Html.DropDownListFor(x => x.ShippingCountry, Model.Countries, "Please select a country",
new
{
#id = "ShippingCountry",
#name = "ShippingCountry",
#class = "w100",
#title = "Shipping Countries"
})
Using something like #Html.labelFor("ShippingCountry") or #Html.LabelFor(m => m.ShippingCountry) does not pass the ADA compliance tests according to the Web Accessibility Evaluation Tool (WAVE).
Is there a specific way to bind labels and form controls such as DropDownListFor in MVC5 using Razor code? Thanks for any help in advance!
I would like to know if it's possible to generate the label tag below using HTML Helpers:
<label class="myClass" attr1="attr1Value" attr2="attr2Value" attr3="attr3Value">labelText</label>
It's basically a standard label tag having 3 extra attributes.
You can use the htmlAttributes parameter of #Html.Label or #Html.LabelFor
#Html.Label("YourLabel", new { attr1 = "attr1Value", attr2 = "attr2Value" })
When using class or other reserved words use at #
#Html.Label("YourLabel", new { #class = "classname" })
When using attributes with dashes -, such as data attributes, use underscores _ which end up being converted to dashes -
#Html.Label("YourLabel", new { data_url = "myurl" })
However, I think support for htmlAttributes in Html.Label and
Html.LabelFor was only added in MVC4, for earlier versions of MVC
you can write your own extension method.
How to extend MVC3 Label and LabelFor HTML helpers?
Using the HtmlHelper its possible to add custom attributes to any Element.
#Html.Label("Content", new { attr1= "attr1" })
but you need to add # to attributes that are keywords e.g. class
I am trying to display the Pound symbol to a radio button in MVC 4, However I am unable to get this ASCII code parsed.
#Html.RadioButtonFor(m => m.Client.CodeID, "true", new { Name = "grp" }) #Html.LabelFor(m => m.ClientID, #Model.Price + Html.Raw("£"))
This works for me -
#Html.RadioButtonFor(m => m.Client.CodeID, "true", new { Name = "grp" })
#Html.LabelFor(m => m.ClientID, #Model.Price + "£")
Let me know if I assumed your question wrongly. My code worked for me IE and Chrome (latest versions)
Try using the HTML symbol for it - "£"