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.
Related
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
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
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?
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;" })
Currently when I want to set html attributes like maxlength and autocomplete, I have to use the following syntax:
<%= Html.TextBox("username", ViewData["username"], new { maxlength = 20, autocomplete = "off" }) %>
Is there any way to do this without having to explicitly set the ViewData["username"] portion? In other words, I want to rely on the helper method's automatic loading routine rather than having to explicitly tell it which field to load up from the ViewData.
Just pass "null" as second parameter:
<%= Html.TextBox("username", null, new { maxlength = 20, autocomplete = "off" }) %>
yes but you have to use ViewData.Model instead of ViewData.Item()
the code in your controller should look like this (sry 4 VB.NET code)
Function Index()
ViewData("Title") = "Home Page"
ViewData("Message") = "Welcome to ASP.NET MVC!"
Dim user As New User
Return View(user)
End Function
now you can do this in the view
<%=Html.TextBox("username", Nothing, New With {.maxlength = 30})%>
note that the user object has a public property username
hth
I used construction as below:
<%= Html.TextBox("username", "", new { #maxlength = "20", #autocomplete = "off" }) %>
For Setting max length of TextBox you can pass "" or null for Second Parameter and set html attributes(maxlength) as third parameter
<%=Html.TextBox("username", "", new { #maxlength = 10 }) %>