为什么没有在linq查询中检查并保存“courseName”?

时间:2018-04-10 08:41:54

标签: c# wpf linq mvvm

我正试图通过我的代码传递一些问题。在我重新实现我的实体框架之后,我正在尝试从注册表单中保存详细信息。我正在使用LINQ查询,我的语法是这样的:

  public void SaveTeacher(object param)
    {

        using (DatabaseStudentsEntitiesLastStand db = new DatabaseStudentsEntitiesLastStand())
        {
            Cours c = new Cours();//this is the Cours table in the db
            c.education = education;//here I'm passing the value from the db to the value from VM.
            RegisterTeacher t = new RegisterTeacher();//this is another table in db where most of the info from registration is stored

            if (isChecked == true || c.courseName != null)
            {
                t.CourseName = c.courseName;//this is the courseName which belongs to courses,but I also have a column in RegisterTeacher for it

            }
            t.SNTeacher = SNTeacher;
            t.UserName = _UserName;
            t.pwd = pwd;
            t.fullName = fullName;


            db.RegisterTeachers.Attach(t);
            db.Courses.Attach(c);
            try
            {
                db.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                foreach (var entityValidationErrors in ex.EntityValidationErrors)
                {
                    foreach (var validationError in entityValidationErrors.ValidationErrors)
                    {
                        MessageBox.Show("Property:" + validationError.PropertyName + "Error:" + validationError.ErrorMessage);
                    }
                }
            }//I'm trying to see the exceptions that I have in case something is not properly bonded(unfortunately no error)

这是与按钮的视图绑定:

     <Button Content="Submit" Command="{Binding SaveCommand}"  
     HorizontalAlignment="Left" Margin="517,98.4,0,0" Grid.Row="2" 
     VerticalAlignment="Top" Width="110" Height="40"/>

最后,将方法传递给delegate命令:

  public RegisterTeacherViewModel()
    {
        mergeCommand = new DelegateCommand(CreateCrazy);
        saveCommand = new DelegateCommand(SaveTeacher);
        IsChecked = true;

    }

    private DelegateCommand saveCommand;
    public DelegateCommand SaveCommand
    {
        get { return saveCommand; }
        set
        {
            if (saveCommand != value)
            {
                saveCommand = value;
                NotifyOnPropertyChange("SaveCommand");
            }
        }
    }

我不知道为什么不工作,但也许这里有人可以向我解释。我也尝试过简单的查询语法,没有任何运气。我根本不会做我所拥有的... 最后但并非最不重要的是,我在xaml.cs中建立了datacontext:

   public partial class Register : Window
{
    RegisterTeacherViewModel regTeacher;
    Object obj = new object();
    public Register()
    {
        InitializeComponent();
        regTeacher = new RegisterTeacherViewModel();
        this.DataContext = regTeacher;
        cbxCourses.ItemsSource = regTeacher.GetByEducation();
        //coursesList.ItemsSource = regTeacher.GetByEducation();
        //regTeacher.CreateCrazy(ShowCourse);
    }

其余的xaml如下:

<Window.DataContext>
    <local:RegisterTeacherViewModel/>
</Window.DataContext>
<Grid Margin="20,20,3.6,28.4">

    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="2*"/>
        <RowDefinition Height="2*"/>
    </Grid.RowDefinitions>

    <Label  Content="Register" 
            Foreground="White" 
            FontSize="25" 
            HorizontalAlignment="Center" 
            Grid.ColumnSpan="2" 
            Margin="179,0,175.6,0.4"/>

    <Separator>
    </Separator>

    <Label  Content="SN:" 
            HorizontalAlignment="Left"
            Margin="10,13.2,0,0" 
            Grid.Row="1" 
            VerticalAlignment="Top"/>

    <Label  Content="Username:&#xD;&#xA;" 
            HorizontalAlignment="Left" 
            Margin="10,63.6,0,0" 
            Grid.Row="1" 
            VerticalAlignment="Top" 
            RenderTransformOrigin="-0.833,-0.208"/>

    <Label  Content="Password:" 
            HorizontalAlignment="Left"
            Margin="10,111.6,0,0" 
            Grid.Row="1" 
            VerticalAlignment="Top"/>

    <Label  Content="Full 
            name:&#xD;&#xA;" 
            HorizontalAlignment="Left" 
            Margin="10,158.2,0,0"
            Grid.Row="1" 
            VerticalAlignment="Top"
            RenderTransformOrigin="0.651,-0.415"/>

    <Label  Content="Courses teaching:&#xD;&#xA;" 
            HorizontalAlignment="Left" 
            Margin="0,229.2,0,0" 
            Grid.Row="1" 
            VerticalAlignment="Top"
            Grid.RowSpan="2"/>

    <TextBox    HorizontalAlignment="Left" 
                Name="SerialNr" 
                Height="23" 
                Margin="126,13.6,0,0"
                Grid.Row="1" 
                TextWrapping="Wrap" 
                Text="{Binding SerialNT}" 
                VerticalAlignment="Top"
                Width="248" 
                Grid.ColumnSpan="2"/>

    <TextBox    Grid.ColumnSpan="2" 
                Name="UsernameTeacher" 
                HorizontalAlignment="Left" 
                Height="23"
                Margin="126,63.6,0,0" 
                Grid.Row="1" 
                TextWrapping="Wrap" 
                Text="{Binding UserName}" 
                VerticalAlignment="Top" 
                Width="248"/>

    <PasswordBox    Name="passwordBox" 
                    mm:PasswordHelper.BindPassword="true"
                    mm:PasswordHelper.BoundPassword="{Binding Path=Pwd, Mode=TwoWay, ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}"
                    PasswordChar="*" 
                    HorizontalAlignment="Left" Height="27"
                    Margin="126,111.2,0,0" 
                    Grid.Row="1" 
                    VerticalAlignment="Top"
                    Width="248"/>

    <TextBox    HorizontalAlignment="Left"  
                Name="fullName" 
                Height="23" 
                TextWrapping="Wrap"  
                Text="{Binding FullName}" 
                VerticalAlignment="Top" 
                Width="248" Grid.ColumnSpan="2" 
                Margin="126,158.6,0,0" 
                Grid.Row="1" 
                Grid.RowSpan="2"/>

    <ComboBox   HorizontalAlignment="Left" 
                DataContextChanged="cbxCourses_DataContextChanged" 
                Text="{Binding Education}" 
                x:Name="cbxCourses" 
                SelectedItem="{Binding Education}" 
                Margin="126,229.2,0,0" 
                IsSynchronizedWithCurrentItem="True" 
                Grid.Row="1" 
                VerticalAlignment="Top" 
                DisplayMemberPath="education"  
                Width="228" 
                Grid.RowSpan="2">

        <!--<StackPanel Orientation="Horizontal">
            <Image Source="https://www.frostburg.edu/fsu/assets/Image/dept/educ/education_sign_resized.jpg" Height="20"></Image>
            <TextBlock Text="Program"></TextBlock>
        </StackPanel>-->

    </ComboBox>//

    <button Content="Submit" 
            Command="{Binding SaveCommand}"  
            HorizontalAlignment="Left" 
            Margin="517,98.4,0,0" 
            Grid.Row="2" 
            VerticalAlignment="Top" 
            Width="110" 
            Height="40"/>

    <Button Content="Cancel"  
            HorizontalAlignment="Left" 
            Margin="361,98.4,0,0" 
            Grid.Row="2" 
            VerticalAlignment="Top"
            Width="111"
            Height="40"/>

    <ListBox    HorizontalAlignment="Left" 
                Name="coursesList" Height="240"   
                Margin="418,13.2,0,0" 
                Grid.Row="1" 
                VerticalAlignment="Top" 
                Width="225"
                Grid.RowSpan="2"  
                ItemsSource="{Binding Courses}">

        <ListBox.ItemTemplate>//here I have a listbox where I have a checkbox with the name of the courses the user wishes to check
            <DataTemplate>
                <CheckBox   x:Name="CheckBoxCourses" 
                            Click="CheckBoxCourses_Click"
                            IsChecked="{Binding Path=IsChecked,UpdateSourceTrigger=PropertyChanged}"
                            Command="{Binding CheckCommand,Mode=TwoWay}"
                            Content="{Binding Path=courseName,Mode=TwoWay}"
                            Margin="0"/>

            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

    <Button Content="Show courses:" 
            Name="ShowCourse"  
            CommandParameter="{Binding ElementName=cbxCourses,Path=SelectedValue}" 
            Command="{Binding MergeCommand}" 
            HorizontalAlignment="Left" 
            Margin="163,61.4,0,0" 
            Grid.Row="2" 
            VerticalAlignment="Top" 
            Width="137" 
            Height="35"/>

</Grid>//this is a button that shows the courses in the listbox after the program was selected

0 个答案:

没有答案