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..."
Related
I am trying to add a drop down select element to my .net page, but I need it to be black so I can populat it with a json list I have with jQuery. Right now, my view has this:
#Html.DropDownList("Language", new SelectList(" "), "name = 'Laguange'", new { #class = "Language-List field form-group form-control" })
This 'works', but it seems to be confusing some stuff when I try to submit the form it is in. The value I select from it is never seen by the model, as it keeps triggering my [Required] validation helper.
Does anyone know How to add this without the new SelectList(" ") attribute?
I would suggest it's because youre using the name attribute in the wrong place.
Try this:
#Html.DropDownList("Language", new SelectList(" "), new {Name="Language", #class = "Language-List field form-group form-control" })
Notice the capital Name
Although as they are the same, you can just leave it out:
#Html.DropDownList("Language", new SelectList(" "), new {#class = "Language-List field form-group form-control" })
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 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" } }
How do I let the RegularExpression accept nothing or only numbers.
At this moment it only accepts numbers. But if I don't enter anything it gives me the standard error: "The Seconds between refreshes field is required."
I want the user to be able to fill in nothing. (My application makes standard values.)
I couldn't find this on other questions. Only white spaces.
[RegularExpression("([1-9][0-9]*)", ErrorMessage = "Test, ErrorMessage from RegularExpression")]
public int NumberOfSecondsBetweenRefresh { get; set; }
My view:
#using (Html.BeginForm("Settings_Personal", "Home", FormMethod.Post))
{
#Html.ValidationSummary()
#Html.LabelFor(x => x.NumberOfSecondsBetweenRefresh)
#Html.TextBoxFor(x => x.NumberOfSecondsBetweenRefresh)
<input type="submit" value="Opslaan" />
}