Issues binding #Html.LabelFor and #Html.DropDownListFor MVC5 - razor

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!

Related

In an ASP.NET View, how do I access my Model's values in Razor and assign it to a variable?

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 })

Using data-number-to-fixed as additionalViewData on Html.EditorFor

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.

how to change a checkbox to 2 radio buttons. Value doesn't pass back 'true'

My table in the DB contains 2 fields. 'Title' and 'IsBusiness'(which is stored as a bool on wether the record is a business account or not.
When adding a new record on screen the editorFor is used to display a checkbox for 'IsBusiness' which passes back true or false.
#Html.EditorFor(x => x.IsBusiness)
#Html.ValidationMessageFor(x => x.IsBusiness)
I want to change this to 2 radio buttons. 'Product' and 'Business' which passes back false if product is selected and true is business is selected.
So far my code below keeps passing back false. It wont store 'True'...any ideas?
<label>#Html.RadioButtonFor(x => x.IsBusiness, "Business")Business</label>
<label>#Html.RadioButtonFor(x => x.IsBusiness, "Product")Product</label>
Try:
<label>#Html.RadioButtonFor(x => x.IsBusiness, true) Business</label>
<label>#Html.RadioButtonFor(x => x.IsBusiness, false) Product</label>
See also: ASP.NET MVC Yes/No Radio Buttons with Strongly Bound Model MVC

Get value of disabled drop down in asp.net mvc

I have an ASP.NET MVC application. I am having multiple drop-down list in my page (HTML SELECT), I have to disable them, as user goes on selected them one by one. When the user posts it back to the controller, I am getting null as the function (action method) paramters. I searched and found that HTML does not send value of disabled fields in the form data. Replacing disabled attribute with readonly would not work as it would render drop-down working.
I am generating the dropdowns dynamically using javascript as user goes on. So there isn't a single dropdown, but as many as user wants.
Can someone please tell me how should I get the values ?
One possibility is to make the dropdown list disabled="disabled" and include a hidden field with the same name and value which will allow to send this value to the server:
#Html.DropDownListFor(x => x.FooId, Model.Foos, new { disabled = "disabled" })
#Html.HiddenFor(x => x.FooId)
If you have to disabled the dropdown dynamically with javascript then simply assign the currently selected value of the dropdown to the hidden field just after disabling it.
This is the default behavior of disabled controls. I suggest you to add a hidden field and set the value of your DropDownList in this hidden field and work with this.
Something like:
//just to create a interface for the user
#Html.DropDownList("categoryDump", (SeectList)ViewBag.Categories, new { disabled = "disabled" });
// it will be send to the post action
#Html.HiddenFor(x => x.CategoryID)
You could also create your own DropDownListFor overload which accepts a bool disabled parameter and does the heavy lifting for you so your view isn't cluttered with if disablethisfield then ....
Something among these lines could do:
public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, bool disabled)
{
if (disabled)
return MvcHtmlString.Create(htmlHelper.HiddenFor(expression).ToString() + htmlHelper.DropDownListFor(expression, selectList, new { disabled="disabled" }).ToString());
else
return htmlHelper.DropDownListFor(expression, selectList);
}
There are 6 overloads for DropDownListFor alone so it's a lot of monkeycoding but it pays off in the end imho.
Create a hidden field with a specified Id and set it before disabling the drop-down-list.
In MVC,
#Html.DropDownListFor(x => x.FooId, Model.Foos)
#Html.HiddenFor(x => x.FooId, new { #id = "hdnFooId" })
In JQuery,
function handleDropDownListFooChange(){
// Get the selected value from drop-down-list before disabling it.
var selectedFooId = $('#FooId').val();
$('#FooId').prop("disabled", "disabled");
$("#hdnFooId").val(selectedFooId);
// Load any data the depends on selected FooId using `selectedFooId` variable.
}
The selected value will automatically be binded to Model.FooId.
before submit call $('#FooId').removeAttr('disabled')

How to set label value on Razor Html.DropDownList

i want to set the label value for a dropdownlist(not the default value, the label value) and i think im doing something wrong
#Html.DropDownList("cboCategoria", new SelectList(Model, "ID", "Nome"), new { #id = "cboCategoria", #label = "Categoria-pai: " })
You can do it a few ways, The label is seperate from the creation of the actual <select>:
<label>Categoria-pai: #Html.DropDownList(...)</label>
OR
<label for="cboCategoria">Categoria-pai:</label> #Html.DropDownList(...)
OR
#* This assumes you are creating the dropdown from a property named
cboCategoria in your Model *#
#Html.LabelFor(m => m.cboCategoria) #Html.DropDownList(...)
EDIT: I did want to note, that if you use the last method, you will want a [Display] attribute on your Model's property.