I am working in an asp.net mvc project and I would like my validation to appear inside of the textbox that it is pertinent to. Currently my code looks like this:
<div class="subStandardOption">
#Html.ValidationMessageFor(model => model.PdsToCreate.Standards.AudioFrequency)
#Html.TextBoxFor(model => model.PdsToCreate.Standards.AudioFrequency)
</div>
The validation only appears the user clicks the checkbox to the left of the text field then tries to submit the form and leaves the textbox is empty, so overwriting the current value is not a concern. When the checkbox is not checked, the textbox is disabled and no validation is necessary.
You can use an HTML5 placeholder to show the required format:
#Html.TextBoxFor(model => model.PdsToCreate.Standards.AudioFrequency, new { placeholder = "e.g. 20 Hz" })
Related
I need to add a validation control in ASP MVC for a textbox. I used the #Html.ValidationMessage helper.
There is one textbox and I want to show the error If the value is not entered in that field.
I used the following code.
#Html.TextBox("txtStudentNumber", "4234234");
#Html.ValidationMessage("txtStudentNumber", "Please enter the student number", new { #class = "text-danger" })
The above message is always shown irrespective if the textbox has a value or not.
I need to validate if the student textbox is empty and also when the save button is clicked.
First of all, you have to use strongly types controls. i.e you should use TextBoxFor instead of TextBox and use ValidationMessageFor instead of ValidationMessage.
and then you have to bind the textbox with model. Try this :
Step 1: Create one model class "ClsStudent"
public class ClsStudent{public int studentNo {get;set;}}
Step 2: Bind the model student number in the view :
#model ClsStudent
#Html.TextBoxFor(m => m.studentNo)
#Html.ValidationMessageFor(m => m.studentNo, null, new { #class = "error" })
Validation on Submit Button :
Submit
I use Kendo validator in my MVC application and want to change the position of validation message from below to right of the input. Although the html code of Kendo UI validation appears right of the input, the same code shows validation message below the input instead of right of it. Is there any mistake or how to convert it to razor? Please note that there is no space between "required" and "validationmessage" in razor code, but when using it as html code an error encountered. Any idea?
You might have a look at: http://jsbin.com/kubohuniki/1/edit?html,output
This line shows validation message on "right" of the input:
<label>Label RIGHT</label>
<input id="Name" class="k-textbox" required validationmessage="required field" />
This line shows validation message on "below" of the input:
<label>Label BELOW</label>
#Html.Kendo().TextBoxFor(m => m.Name, new { #class="k-textbox", requiredvalidationmessage="required field" })
try this (updated)
#Html.TextBoxFor(m => m.Question, new { #class = "k-textbox",
required = "true", validationmessage = "required field" })
#Html.ValidationMessageFor(model => model.Question)
I have simple edit form, where you can edit user profile, but you cannot edit username.
So instead this code:
#Html.EditorFor(model => model.username)
I use this line:
#Html.TextBoxFor(model => model.username, new {disabled = "disabled", #readonly = "readonly" })
This works fine, but, I cannot save other fields which are "EditorFor"
e.g. I have column Customers, so one user can have one or more customers, so if I make change in edit, e.g. add new user to customer, and then when I click save, it does not save it. But if I change code again to #Html.EditorFor(model => model.username) then it save it...
Any idea how to fiks this?
The problem is that a read only textbox is not sent back (value is not in POSTed datas).
So if you want to have a readonly textbox AND have the value of username in POSTed datas, you'll have to add a
#Html.HiddenFor(m => m.username)
But... if you just wanna display them, you should just manage that in your controller (don't try to update username... as you don't wanna update username).
I have in my edit form displayed: Username, TimeZone, Customer...
I don't whant to be able to edit username, just display his name.
This code I use in View:
<label>Username </label>
<div class="editor-field">
#Html.EditorFor(model => model.username)
</div>
So what to put instead EditorFor, that will display username (just for reading, not for editing).
Why not just use #Html.DisplayFor instead? This will just display the username as a label. Or, if you wish to use #Html.EditorFor or #Html.EditorForModel, you can create a custom editor template for your username property, and in the editor template, just display the content instead of enabling editing.
Also, I would recomment you exclude this property during model binding by using [Bind(Exclude="username")] with your model parameter in your POST action method, to protect from injection attacks. More about this here.
find solution:
#Html.TextBoxFor(model => model.username, new {disabled = "disabled", #readonly = "readonly" })
I have one partial view with two TextBoxFor<>. I notice that when I run my application in browser that text box remembers and display old entered values in text box. I want display clear text box but can't find why this is happening. I have tried with:
ModelState.Clear();
In post method but doesn't solve anything.
<td>#Html.TextBoxFor(x => x.Username, new { #class = "loginUsername", placeholder = "Email" })
</td>
<td>#Html.TextBoxFor(x => x.Password, new { #class = "loginPassword", placeholder = "Password", type = "password" })
</td>
It might be due to a remember password feature of your browser being activated which automatically saves all username/password values on the client. You could append the autocomplete="off" attribute on your password field to avoid this from happening. This attribute is not standards compliant but most browsers respect it.