Home > Enterprise >  How to prevent old data in Model from being passed into the input fields automatically?
How to prevent old data in Model from being passed into the input fields automatically?

Time:11-29

Is it possible to prevent the data from being automatically passed to the input fields. For example - if I have an edit page where I want to change the data and I want the user to type the data again without giving him the pre-filled old data. I was looking over some documentation about the ModelState and the asp-for tag but I just couldn't find the answer.

I was looking for help and I found this post here. Well I want to do exactly the opposite.

asp-for tag helper functionality_

EDIT: I tried to change the value attribute of the input field to empty string and it worked, however I still would like to know if there is an asp.net approach.

CodePudding user response:

If you don't want to pass data from action to Edit view,you can return a view with an empty model like this:

public IActionResult Edit()
        {
            return View(new YourModel());
        }

Or you can try to use id and name attribute to replace asp-for,so that the data will not be binded to inputs.Here is a demo.

Change

<input asp-for="TestProperty" />

to

<input id="TestProperty" name="TestProperty" />
  • Related