I get a duplicate name attribute while validating through https://validator.w3.org
#Html.EditorFor(model => model.User_Name, new { htmlAttributes = new { #class = "form-control", id = "txtFullNameRealtor", #Name = "txtFullNameRealtor", placeholder = "Full Name" } })
I get this in the html source
<input Name="txtFullNameRealtor" class="form-control text-box single-line" id="txtFullNameRealtor" name="User_Name" placeholder="Full Name" type="text" value="" />
As you can see there,two name attributes are generated that is Name="txtFullNameRealtor" and name="User_Name".
Is there a way to make it generate a single name attribute ?
And I want the Name attribute be explicitly set by me to be there.
You could acheive this with the following:
#Html.TextBox("txtFullNameRealtor", Model.User_Name, new { #class = "a-class-name" })
As it looks like you want a text box anyway you can use the text box helper. The first argument is where the name/id are derived from, the second argument is the value and the 3rd are the htmlAttribtues. There are more overloads as well.
User Html.Editor() instead of Html.EditorFor().
EditorFor will infer the html element name from the model property that is "bound" to it.
Editor allows you to declare whatever html name you would like with the last parameter.
public static MvcHtmlString Editor(
this HtmlHelper html,
string expression,
string templateName,
string htmlFieldName
It sounds like you are binding a domain model directly to your UI. I'd suggest create a viewmodel with the property names you want and translating between your view model and domain model in your controller instead.
You can try this
#Html.EditorFor creates name same as that of property name.
if you want to specific name you can use #Html.Editor instead of #html.EditorFor. i hope this helps
#Html.Editor("txtFullNameRealtor" er_Name, new { htmlAttributes = new { #class = "form-control", id = "txtFullNameRealtor", placeholder = "Full Name" } }
Related
I am displaying a simple model value in view page as : #Model.Name
and I need to include an id and a name in it.
I can include id and name in Html.TextBoxFor as :
#Html.TextBoxFor(model => model.Name, new { #class = "form-control bg-white", #placeholder = "Name", #id="Name", #name="Name" })
Similarly I need id and name in #Model.Name as well .
Can anyone Help me ?
Thanks.
You could achieve the same result using:
#Html.HiddenFor(model => model.Name)
#Html.TextBoxFor(model => model.Name, new { htmlAttributes = new { #class = "form-control", id = "Name", name = "Name" }})
i think this can solve your problem
Nevermind, I didn't find the answer I was looking for and hence, I used an alternative way to send my 'Name' value as follows :
#Model.Name
<input type="text" name="name" value="#Model.Name" hidden />
I created an input type and made it hidden so that I can later(while submitting form) extract value via name attribute.
You have to change name of property.
I can't get id attribute with "CaseNumber", but I get it rename property to "CrimeNumber".
I want to change the class/style of my #Html.EditorFor field. I read something about customizing the template, but there is no template folder in my porject (Views/Shared/ONLY_FILES_HERE). Sadly I am NOT working with MVC 5.1.
Also I DON'T want to use TextBoxFor, because of inputvalidation.
How can I achieve this?
Here is a snippet of my model:
public class CreateTableColumnModels
{
[RegularExpression(#"^[A-Za-z0-9]+$", ErrorMessage = "Use letters and numbers only please")]
[Display(Name = "Name")]
public string Name { get; set; }
I tried this:
#model QlikViewStammdaten.Models.CreateTableColumnModels
<div>
#Html.EditorFor(x => Model.Name, new { #class = "form-control" })
</div>
As already mentioned in the comments, it is better to add a class to Html.TextBoxFor and NOT #Html.EditorFor. Why? because #Html.EditorFor renders a template and that template could contain multiple controls (now to which one of those controls do you want to add the class?)... see Adding a class to EditorFor
If you want numeric type, then add #type = "number"
#Html.TextBoxFor(m => m.Name, new { #class = "form-control", #type = "number" })
Note: it is not x => Model.Name but m => m.Name
I am sure you already know this, but [Display(Name = "Name")] is redundant in your model, as the default display name is the variable name itself.
I am using mvc5 in combination with razor and fill website text dynamically based on language. example:
#Html.Label(#DDHelper.GetContent("user_name"), htmlAttributes: new { #class = "control-label col-md-3" })
#DDHelper.GetContent("user_name") returns a string, which is set as a label text. when #DDHelper.GetContent("user_name") returns "Hello." the label is not created and the html source is empty.
I know this is because #HTML.Label() does not allow '.' and that's the root of my problem and that I should use #HTML.LabelFor() but how can i use #HTML.LabelFor() when i just want to display a string?
Check the overloads for #Html.Label, the one you're using is:
public static MvcHtmlString Label(this HtmlHelper html,
string expression,
object htmlAttributes)
the one you want is
public static MvcHtmlString Label(this HtmlHelper html,
string expression,
string labelText,
object htmlAttributes)
There's a common misunderstanding of what an HTML label is. On winforms, a label is where you put some text - this is not the case in HTML. In HTML a <label> is to allow the user to click on your text and have the focus/cursor point to the corresponding input.
The full syntax for a label is:
<label for="controlName">caption</label>
The 'expression' part in the overloads above is the "for" part in the html - it must point to a control name.
If this is what you are trying to do (pair a label with a control) then try:
#Html.Label("user_name",
#DDHelper.GetContent("user_name"),
htmlAttributes: new { #class = "control-label col-md-3" })
Where your input is named 'user_name'. This is where #Html.LabelFor comes in, something like:
#Html.Labelfor(model=>model.UserName,
#DDHelper.GetContent("user_name"),
htmlAttributes: new { #class = "control-label col-md-3" })
so you don't hard-code field names and they refactor.
If you "just want to display a string" then you probably don't want a <label>. If you just need some simple text, you don't need an #Html anything and can just output the translation:
<div class='control-label col-md-3'>
#DDHelper.GetContent("user_name")
</div>
but as you're using 'control-label' I suspect you do want <label>.
If Html.Label(..) is not an option I'd suggest an alternative:
<h4>#Html.Encode(#DDHelper.GetContent("user_name"))</h4>
Worked fine for me.
I have the following textboxfor in my view
#Html.TextBoxFor(m => m.AdvertModel.Title, new
{
#class = "form-control",
type = "text",
placeholder = "Enter a descriptive title about the item for sale",
id = "Enter a descriptive title",
data_val_required = "The Title field is required.",
data_val = "true"
})
You can see I have added the data_val_required and data_val attributes to do it, this renders as follows:
<input id="Enter a descriptive title" class="form-control" type="text" value="" placeholder="Enter a descriptive title about the item for sale" name="Title" data-val-required="The Title field is required." data-val-maxlength-max="100" data-val-maxlength="The field Title must be a string or array type with a maximum length of '100'." data-val="true">
When I run the application and this and leave it empty and click submit the ModelState.isValid is always true, when I would expect it to be false, why does it keep saying it true?
When you submit a form to a POST method, the form values contain key/value pairs consisting of each controls name attribute and value attribute. In your case it would be AdvertModel.Title=The value entered in the textbox. No information regarding other attributes in controls are sent to the server.
The data-* attributes are rendered by the html helpers based on validation attributes applied to the model and are useful only if you have the associated #Html.ValidationMessageFor() and include the relevant script files (jquery, jquery.validate and jquery.validate.unobtrusive).
You will get both server side and client side validation if you include the [Required] attribute on the model property
[Required(ErrorMessage = "The Title field is required.")]
public string Title { get; set; }
and in the view
#Html.TextBoxFor(m => m.AdvertModel.Title, new { #class = "form-control", placeholder = "Enter a descriptive title about the item for sale", title = "Enter a descriptive title" })
#Html.ValidationMessageFor(m => m.AdvertModel.Title)
Side notes: You do not need type="text" (this is added by the helper) and I assume id = "Enter a descriptive..." is a typo and that its really title = "Enter a descriptive..."
I want to set ID and Text attribute in html.label helper in mvc2
<%:html.label<have to set ID and Text properties here>%>
Plz help me out..
The Html.Label method returns an HTML label element and the property name of the property that is represented by the specified expression. For example:
ASPX Syntax
<%: Html.Label("Text Content", new { id = "labelId" })%>
Razor Syntax
#Html.Label("Text Content", new { id = "labelId" })
The second parameter is the htmlAttributes, so, you can add any html attribute you want as a property of this anonymous object. For example:
new { id = "id-element", name = "name-element", size = 10, #class = "css-class" }
IdFor
If you want to take the Id by a html helper method, try to use:
#Html.IdFor(model => model.Property)