Razor input element custom attribute - asp.net-mvc

Hi I need to do the following:
#Html.TextBoxFor(m => m.Login,
new {
#class = "form-control",
#placeholder = "Username",
required="true" data-required-message="Please insert your name"
})
But Im getting error on data-required-message seems that I can't use "-".
Any clue?

You have to use it with underscore _ and razor will render it as - in html:
data_required_message="Please insert your name"
another thing to note is that you don't need to put # sign in front of every parameter of htmlAttributes , we put it for class becasue class is a reserved word in c#, so it cannot be used directly as a variable name.
your final code will be like:
#Html.TextBoxFor(m => m.Login,
htmlAttributes: new {
#class = "form-control",
placeholder = "Username",
required="true",
data_required_message="Please insert your name"
})
you are also missing a comma after placeholder attribute.

Try data_required_message instead of data-required-message

Related

The reason for using # in the razor extension attributes

Both of these codes have the same result in the final markup. so why is the # used in the htmlAttributes section?
#Html.TextBoxFor(model => model.ManagerName, new { #autocomplete = "off", #maxlength = "40" })
#Html.TextBoxFor(model => model.ManagerName, new { autocomplete = "off", maxlength = "40" })
Does the MVC version have an effect on this?
It may have been mandatory in older versions!
In the "new { ... }" block, some words such as "class" are keywords. # is required in order to escape the keyword. For non-keywords, it doesn't make a difference.

disable Html.DropDownListFor

I have the below code in CSHTML but seems like the dropdown is not getting disabled
#Html.DropDownListFor(x => x.Task_Status_Code, Model.TaskStatus, new { #class = "form-control", #disabled = "disabled" })
I want to disable it but it is not working.
#Html.DropDownListFor(
x => x.Task_Status_Code,
Model.TaskStatus,
new { #class = "form-control", disabled = "disabled" })
Removed the # sign before the disabled property name.
You need to add the # character before any dotnet keywords like class. Custom properties do not need to have # prefix.
Please check with below snippet !
#Html.DropDownListFor(
x => x.ReminderTime,
Model.RemindersList,
new { disabled = "disabled" }
)
#Html.DropDownListFor(x => x.Task_Status_Code, Model.TaskStatus, new { disabled = "disabled" })
That will work as well unless you define css form control with in then
#Html.DropDownListFor(x => x.Task_Status_Code, Model.TaskStatus, new { #class="form-control", disabled = "disabled" })
Remember not to put #sign before disabled

Possible to change LabelFor?

Say if I have this textbox
#Html.TextBoxFor(x => x.Name, new {id = "add-name", data_parsley_maxlength = "1" })
How do I write my LabelFor so that it references "add-name"?
Or do I have to use #Html.Label?

Add class in dropdown list

#Html.DropDownListFor(m => m.Baled, new SelectList(new [] {"Select Type","Option1", "Option2", "Option3"}))
I want to add a css class in the dropdown list.
I have tried this
#Html.DropDownListFor(m => m.Baled, new SelectList(new [] {"Select Type","Option1", "Option2", "Option3"}, new {#class="myclass"}))
But it doesn't work
If anyone can suggest me any other better way to display dropdown list will be ok too.
You have close the bracket at wrong place. you have defined attribute in second paramter of selectlist. actually it should be close first and define in the last parameter of Html.DropDownListFor
#Html.DropDownListFor(m => m.Baled, new SelectList(new[] { "Select Type", "Option1", "Option2", "Option3" }), new { #class = "myclass" })

How to use trim on DropdownlistFor

Benefit Set: </label><br />
<%: Html.DropDownListFor(model => model.bvODSMapping.Benefit_Set, Model.BenefitSet,new {id="BSet", style = "width:230px;" })%>
When I am using model=>model.bvODSMapping.Benefit_Set.Trim()
I am getting Value Can not be null.
can anybody help me out how to trim the string?
Thanks
I guess it would be the best to trim the values before passing the model to your view.
Nevertheless, this might help:
#Html.DropDownListFor(
model =>
model.bvODSMapping.Benefit_Set,
Model.BenefitSet.Select(
item =>
new SelectListItem
{
Selected = item.Selected,
Text = item.Text.Trim(),
Value = item.Value
}),
new { id = "BSet", style = "width:230px;" })

Resources