简体   繁体   中英

Model that doesn't write pre-populate value to view

I am using the model class as below

public class Question
{
    public long ID { get; set; }
    public int QuestionNo { get; set; }
    public string Title { get; set; }
    public QuestionTypes QuestionType { get; set; }
    public List<Answer> Answers { get; set; }
}

I created a text box in view as below.

@Html.TextBoxFor(Model => Model.ID);

it pre-populate ID with the values provided which is not I want. I want text box should be empty when rendered and during POST value of the text box should be read by ID property. How can I achieve this or there is a problem with my design?

okay,,

if the Question object you're passing to the View is already initialized, then it will have some value in ID property and that will be shown in the view when you do databindings in the view

So before passing the Question object to the view , set the value of ID to null.

in this way , when your Question will go to view, it will have null value and the textboxfor will be empty

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM