ASP MVC TextBoxFor<> somehow remember old entered values - asp.net-mvc

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.

Related

How to save the user email address into 2 different textboxes

Basically I have 2 textboxes; I would like whatever the user types into the email textbox to be saved into the name textbox as well.
Basically saving the email address in 2 places. I’m new to MVC any examples would be good, cheers.
#Html.TextBoxFor(m => registerModel.Name, new { #class = "form-control" })
#Html.TextBoxFor(m => registerModel.Email, new { #class = "form-control" })
If you want to save the same values for the both text fields, you can assign same value to both on post function, there is no need for an additional text field.
But if you want to display same value for both text box while typing on one text box. Then try following javascript.
$("#Name").keyup(function(){
var Names = $("#Name").val();
$("#Email").val(Names);
})

autocomplete=off not working in mvc 5

I am trying not to generate autocomplete in browser for that I am trying this
#Html.TextBoxFor(
model => model.UserName,
new { #class = "form-control", autocomplete = "off" })
#Html.ValidationMessageFor(m => m.UserName, "", new { #class = "text-danger" })
which generate HTML like this
<input type="text" value="" name="UserName" id="UserName" data-val-required="User name is required." data-val="true" class="form-control" autocomplete="off">
In other views I have use the same and it is working fine. but in login view autocomplete="off" not working. for doing so I have clear model state like this in controller
ModelState.Remove("UserName");
reference taken from the following sites
How to Turn Off Form Autocompletion
Disable autocomplete on html helper textbox in MVC
How to disable autocomplete in MVC Html Helper
Remove browser autocompletion in MVC
I still wonder what is wrong in my code? Your suggestion means a lot to me.
As per Bharat's answer it actually, the issue is not of the autocomplete,
the issue comes from the browser's saved logins.
but we can overcome this without going to clear saved passwords of the browser.
As per my intention, all browsers doing similar duties that the field before the password is assumed as Username and it uses the last logged in data will be pasted automatically even if we do autocomplete="off".
The solution is to make the password's field autocomplete="new-password", but this will work only in HTML5 or above supported browsers only...
The issue comes from the browser's saved logins.
When these issue arise at my side, i removed saved logins from my browser and it solved my issue.
visit https://support.mozilla.org/en-US/questions/962690
https://support.google.com/chrome/answer/95582?hl=en
It is Working in MVC 5, to auto-complete off
#Html.TextBoxFor(model => model.UserName,
new { #placeholder = "UserName", #id = "txtUserName", #autocomplete = "new-user" })
#Html.PasswordFor(model => model.Password,
new {#placeholder = "Password", #id = "txtPassword", #autocomplete = "new-password" })

Display name instead editor field in MVC4

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" })

How do I make validation text appear inside of a textbox?

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" })

ASPNET MVC: Retrieve Field Id in Javascript for non-Model fields

I have 2 fields in a MVC view.
One is #Html.TextBox("txtFirstName")
Second is hidden - #Html.TextBoxFor(model => model.FirstName, new { #type = "hidden" })
The hiddenField is there for posting purposes (as the firstTextbox can be disabled as times).
My question is How do I retrieve the client ID of first Textbox in Javascript ?? I can easily access the client ID of second textbox by '#Html.FieldIdFor(m => m.FirstName)'
The client ID has the modelName prefixed and I dont want to do any hardcoding in Javascript code.
In this case it will be txtFirstName.
But you could explicitly set it:
#Html.TextBox("txtFirstName", "some sample value", new { id = "txtFirstName" })
and then access it as usual:
var value = document.getElementById('txtFirstName').value;
or if you are using jQuery:
var value = $('#txtFirstName').val();
Use HTML helper inside your Javascript:
Javascript & Razor:
var textboxId = '#Html.IdFor(m => m.FirstName)';
There are several extensions:
#Html.IdFor(m => m.FirstName)
#Html.NameFor(m => m.FirstName)
Here is the full list of HTML helper methods http://msdn.microsoft.com/en-us/library/system.web.mvc.htmlhelper_methods(v=vs.108).aspx
Good luck

Resources