如何在asp.net-mvc3中更新两个或多个实体? [英] How can I update two or more entities in asp.net-mvc3?

查看:41
本文介绍了如何在asp.net-mvc3中更新两个或多个实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新两个这样的实体:

Hi, I’m trying to update two entities like this:

for (int i = 0; i < numberOfRows i++)
 {

     var enrollment = db.Enrollments.SingleOrDefault(x => x.EnrollmentID == i+1);
     var student = db.Students.SingleOrDefault(x => x.StudentID == i+1);

     student.FirstMidName = AllFstMNames[i];
     enrollment.Grade = Convert.ToInt32(AllGrades[i]);

     student.EnrollmentDate = Convert.ToDateTime(AllEnrollmentDates[i]);

    if (!string.IsNullOrEmpty(frmcol["item.Grade"]))
                    {
                        enrollment.Grade = Convert.ToInt32(AllGrades[i]);


                    }
     enrollment.StudentID = Convert.ToInt32(AllStudentIds[i]);
     enrollment.attendanceCode = Convert.ToInt32(AllAttendanceCodes[i]);
//more code that assign form values to entity properties.

     db.SaveChanges();
 }


我想要的是仅将更改应用于视图中更改的字段.我该怎么办?

感谢您的帮助!


What I want is to have changes applied only to the fields that I change in the view. How can I accomplish this?

Thanks for any help!

推荐答案

这应该可以正常工作.您从数据库中获取了值,并进行了更改,我想这就是您要设置的更改.那么,问题是什么呢?如果您仅更改了某些内容,并且由于某些原因显示的其他值不正确,那么显然您需要跟踪更改了哪些项目.
This should just work. You get the values from the DB, and the changes you made, I assume are the ones you''re setting. So, what''s the issue ? If you only changed some, and if the other values you''re showing are not correct for some reason, then you need to track which items were changed, obviously.


这篇关于如何在asp.net-mvc3中更新两个或多个实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆