简体   繁体   English

不写预填充值即可查看的模型

[英]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. 它使用提供的值预填充ID ,这不是我想要的。 I want text box should be empty when rendered and during POST value of the text box should be read by ID property. 我希望文本框在呈现时应该为空,并且在POST期间应该通过ID属性读取文本框的值。 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 如果您要传递给View的Question对象已经初始化,那么它将在ID属性中具有某些值,并且在视图中进行数据绑定时将在视图中显示该值

So before passing the Question object to the view , set the value of ID to null. 因此,在将Question对象传递到视图之前,将ID的值设置为null。

in this way , when your Question will go to view, it will have null value and the textboxfor will be empty 这样,当您的问题将要查看时,它将具有空值,并且textboxfor将为空

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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