模型绑定以在 MVC 中查看

发布于 2024-09-25 00:44:09 字数 1172 浏览 5 评论 0原文

我是 MVC 的初学者,想知道如何通过绑定模型值设置回来以供查看。这是一个例子。

public class DataTypes
{
    public Guid ItemID { get; set; }
    [Required()]
    public string Name { get; set; }
    [Required()]
    public string Status { get; set; }
    [Required()]
    public DataModel DataModel { get; set; } // This is for Binding
}
public class DataModel
{
    public string Activity { get; set; }
    public DateTime ?DateTime { get; set; }        
}

通过上面的模型类,我可以成功地将数据从 UI 绑定到后端,但问题是如何使用上面的方法将相同的数据重新运行到 UI。我尝试了下面的代码,但是当涉及到设置绑定类(DataModel)的值时,

        this.dataType.ItemID = // Guid from stored vaule in DataBase
        this.dataType.Name = // Name from stored vaule in DataBase
        this.dataType.Status = // Status from stored vaule in DataBase

                        // Set the activity to UI - ERROR.....!!!!!!
                        // Error was NullReferenceException unhandled
        this.dataType.DataModel.Activity = // Activity from stored vaule in DataBase
        this.dataType.DataModel.DateTime = // DateTime from stored vaule in DataBase

        return View(this.dataType);

有解决上述问题的方法吗?

预先感谢, 高压

I am a beginner to MVC and would like to know how I can set by binded model vaule back for viewing. Here is the example.

public class DataTypes
{
    public Guid ItemID { get; set; }
    [Required()]
    public string Name { get; set; }
    [Required()]
    public string Status { get; set; }
    [Required()]
    public DataModel DataModel { get; set; } // This is for Binding
}
public class DataModel
{
    public string Activity { get; set; }
    public DateTime ?DateTime { get; set; }        
}

With the above model class, I am sucessfully able to bind data from UI to backend but the problem is that how I can retrun the same data to UI using the above. I tried the below code but when it comes to setting the vaules for Binded class (DataModel)

        this.dataType.ItemID = // Guid from stored vaule in DataBase
        this.dataType.Name = // Name from stored vaule in DataBase
        this.dataType.Status = // Status from stored vaule in DataBase

                        // Set the activity to UI - ERROR.....!!!!!!
                        // Error was NullReferenceException unhandled
        this.dataType.DataModel.Activity = // Activity from stored vaule in DataBase
        this.dataType.DataModel.DateTime = // DateTime from stored vaule in DataBase

        return View(this.dataType);

Any work around for the above issue?

Advance Thanks,
HV

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不乱于心 2024-10-02 00:44:09

您似乎忘记实例化 this.dataType.Datamodel:

this.dataType.DataModel = new DataModel();
this.dataType.DataModel.Activity = // Activity from stored vaule in DataBase
this.dataType.DataModel.DateTime 

It appears that you forgot to instantiate this.dataType.Datamodel:

this.dataType.DataModel = new DataModel();
this.dataType.DataModel.Activity = // Activity from stored vaule in DataBase
this.dataType.DataModel.DateTime 
街道布景 2024-10-02 00:44:09
    Controller class
    
//All Controls below here

    public Controller(Stage mainStage, Scene mainScene){
            this.mainStage = mainStage;
            this.mainScene = mainScene;
            getReferences();
            initialiseData();
            setBindings();
            setEventHandlers();
        }
    
        private void setBindings(){
            tfName.textProperty().bind(curStudent.nameProperty());
            tfSurname.textProperty().bindBidirectional(curStudent.surnameProperty());
            tfAge.textProperty().bindBidirectional(curStudent.ageProperty(), new NumberStringConverter());
            sdAge.valueProperty().bindBidirectional(curStudent.ageProperty());
            tfNumber.textProperty().bindBidirectional(curStudent.numberProperty(), new NumberStringConverter());
            lbPosition.textProperty().bind(Bindings.concat(currentIndex.add(1)).concat(" of ").concat(students.size()));
        }
        private void changeBindings(Student oldStudent, Student newStudent){
            if(oldStudent != null) {
                tfName.textProperty().unbindBidirectional(oldStudent.nameProperty());
                tfSurname.textProperty().unbindBidirectional(oldStudent.surnameProperty());
                tfAge.textProperty().unbindBidirectional(oldStudent.ageProperty());
                tfNumber.textProperty().unbindBidirectional(oldStudent.numberProperty());
                sdAge.valueProperty().unbindBidirectional(oldStudent.ageProperty());
    
                tfName.textProperty().bindBidirectional(newStudent.nameProperty());
                tfSurname.textProperty().bindBidirectional(newStudent.surnameProperty());
                tfAge.textProperty().bindBidirectional(newStudent.ageProperty(), new NumberStringConverter());
                tfNumber.textProperty().bindBidirectional(newStudent.numberProperty(), new NumberStringConverter());
                sdAge.valueProperty().bindBidirectional(newStudent.ageProperty());
            }
        }
        private void setEventHandlers(){
            btPrev.setOnAction(actionEvent -> {
                if(currentIndex.get() > 0){
                    if(btNext.isDisable())
                        btNext.setDisable(false);
                    currentIndex.setValue(currentIndex.get() - 1);
                    if(currentIndex.get() == 0)
                        btPrev.setDisable(true);
                    Student newStudent = students.get(currentIndex.get());
                    changeBindings(curStudent,newStudent);
                    curStudent = newStudent;
                }
            });
            btNext.setOnAction(actionEvent -> {
                if(currentIndex.get() < students.size()-1){
                    if(btPrev.isDisable())
                        btPrev.setDisable(false);
                    currentIndex.setValue(currentIndex.get() + 1);
                    if(currentIndex.get() == students.size()-1)
                        btNext.setDisable(true);
                    Student newStudent = students.get(currentIndex.get());
                    changeBindings(curStudent, newStudent);
                    curStudent = newStudent;
                }
            });
    
            btNew.setOnAction(actionEvent -> {
                Student newStudent = new Student("?","?",0,1);
                students.add(newStudent);
                changeBindings(curStudent, newStudent);
                curStudent = newStudent;
                currentIndex.setValue(students.size() - 1);
                lbPosition.textProperty().bind((StringBinding) Bindings.concat(currentIndex.add(1)).concat(" of ").concat(students.size()));
                btNext.setDisable(true);
            });
        }
    Controller class
    
//All Controls below here

    public Controller(Stage mainStage, Scene mainScene){
            this.mainStage = mainStage;
            this.mainScene = mainScene;
            getReferences();
            initialiseData();
            setBindings();
            setEventHandlers();
        }
    
        private void setBindings(){
            tfName.textProperty().bind(curStudent.nameProperty());
            tfSurname.textProperty().bindBidirectional(curStudent.surnameProperty());
            tfAge.textProperty().bindBidirectional(curStudent.ageProperty(), new NumberStringConverter());
            sdAge.valueProperty().bindBidirectional(curStudent.ageProperty());
            tfNumber.textProperty().bindBidirectional(curStudent.numberProperty(), new NumberStringConverter());
            lbPosition.textProperty().bind(Bindings.concat(currentIndex.add(1)).concat(" of ").concat(students.size()));
        }
        private void changeBindings(Student oldStudent, Student newStudent){
            if(oldStudent != null) {
                tfName.textProperty().unbindBidirectional(oldStudent.nameProperty());
                tfSurname.textProperty().unbindBidirectional(oldStudent.surnameProperty());
                tfAge.textProperty().unbindBidirectional(oldStudent.ageProperty());
                tfNumber.textProperty().unbindBidirectional(oldStudent.numberProperty());
                sdAge.valueProperty().unbindBidirectional(oldStudent.ageProperty());
    
                tfName.textProperty().bindBidirectional(newStudent.nameProperty());
                tfSurname.textProperty().bindBidirectional(newStudent.surnameProperty());
                tfAge.textProperty().bindBidirectional(newStudent.ageProperty(), new NumberStringConverter());
                tfNumber.textProperty().bindBidirectional(newStudent.numberProperty(), new NumberStringConverter());
                sdAge.valueProperty().bindBidirectional(newStudent.ageProperty());
            }
        }
        private void setEventHandlers(){
            btPrev.setOnAction(actionEvent -> {
                if(currentIndex.get() > 0){
                    if(btNext.isDisable())
                        btNext.setDisable(false);
                    currentIndex.setValue(currentIndex.get() - 1);
                    if(currentIndex.get() == 0)
                        btPrev.setDisable(true);
                    Student newStudent = students.get(currentIndex.get());
                    changeBindings(curStudent,newStudent);
                    curStudent = newStudent;
                }
            });
            btNext.setOnAction(actionEvent -> {
                if(currentIndex.get() < students.size()-1){
                    if(btPrev.isDisable())
                        btPrev.setDisable(false);
                    currentIndex.setValue(currentIndex.get() + 1);
                    if(currentIndex.get() == students.size()-1)
                        btNext.setDisable(true);
                    Student newStudent = students.get(currentIndex.get());
                    changeBindings(curStudent, newStudent);
                    curStudent = newStudent;
                }
            });
    
            btNew.setOnAction(actionEvent -> {
                Student newStudent = new Student("?","?",0,1);
                students.add(newStudent);
                changeBindings(curStudent, newStudent);
                curStudent = newStudent;
                currentIndex.setValue(students.size() - 1);
                lbPosition.textProperty().bind((StringBinding) Bindings.concat(currentIndex.add(1)).concat(" of ").concat(students.size()));
                btNext.setDisable(true);
            });
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文