0

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

2 Answers 2

1

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 
Sign up to request clarification or add additional context in comments.

Comments

0
    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);
            });
        }

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.