[英]How to Update Two Tables using LINQ in ASP.NET MVC 4 Web Applications
I'm making Library Management System project in which I have a Deposit
Controller which deposits the earlier issued books. 我正在制作“图书馆管理系统”项目,其中有一个“
Deposit
控制器”,用于存放较早发行的书籍。 Two tables are used: 使用两个表:
BooksData
has all book info with a BookStatus
Flag which sets 'A' for Available and 'I' for Issued. BooksData
具有BookStatus
标志的所有书籍信息,该标志将“ A”设置为“可用”,将“ I”设置为“已发行”。 BookDepositIssue
has BookID
which is common in both tables and IssueDate, DepositDate, and DueDate
. BookDepositIssue
具有BookID
,这在表和IssueDate, DepositDate, and DueDate
和IssueDate, DepositDate, and DueDate
中都是通用的。
Now in my deposit controller I'm checking if the BookStatus
flag is "I"; 现在在我的存款控制器中,我正在检查
BookStatus
标志是否为“ I”; which means Issued. 表示已发行。 And also
DueDate<= DateTime
.Today so that no fine is incurred. 还有
DueDate<= DateTime
.Today,因此不会产生罚款。 But I'm unable to make the correct code pls help. 但是我无法提供正确的代码帮助。 As shown by the debugger var v is getting null, thus not going into the if condition.
如调试器所示,var v为空,因此不会进入if条件。 The database has issued books whose flag is "I" then why it is not executing the condition?
数据库已发布标记为“ I”的书籍,那么为什么它不执行该条件?
Controller part 控制器部分
[HttpPost]
public ActionResult Deposit (BookDepositIssue deposit)
{
if (ModelState.IsValid)
{
using (LibraryEntities dc = new LibraryEntities())
{
var v = dc.BooksDatas.Where(a=> a.BookStatus.Equals("I") && a.BookID.Equals(deposit.BookID) && deposit.DueDate<=DateTime.Today).FirstOrDefault();
if(v!=null)
{
v.BookStatus="A";
deposit.depositDate = DateTime.Today;
}
UpdateModel(dc.BooksDatas);
UpdateModel(dc.BookDepositIssues);
dc.SaveChanges();
}
}
}
Model Part 模型零件
Model class 1:
public partial class BookDepositIssue
{
public int SequenceNo {get; set;}
public string BookID {get; set;}
public string ID {get; set;}
public Nullable<System.DateTime> IssueDate {get; set;}
public Nullable<System.DateTime> DueDate {get; set;}
public Nullable<System.DateTime> DepositDate {get; set;}
}
Model Class 2:
public partial class BooksData
{
public string BookID {get; set;}
public BookName {get; set;}
public BookGenre {get; set;}
public BookStatus {get; set;}
public Author {get; set;}
}
try this - assuming you have a navigation property between BookDepositIssue
to 'BookDepositIssue' named bookData
. 尝试执行此操作-假设您在
BookDepositIssue
和'BookDepositIssue'之间有一个名为bookData
的导航属性。 if you don't have one, you should. 如果您没有,那应该。
replace this part: 替换此部分:
var v = dc.BooksDatas.Where(a=> a.BookStatus.Equals("I") && a.BookID.Equals(deposit.BookID) && deposit.DueDate<=DateTime.Today).FirstOrDefault();
if(v!=null)
{
v.BookStatus="A";
deposit.depositDate = DateTime.Today;
}
UpdateModel(dc.BooksDatas);
UpdateModel(dc.BookDepositIssues);
dc.SaveChanges();
with this: 有了这个:
if(deposit.bookData.BookStatus == "I" && deposit.DueDate<=DateTime.Today){
deposit.bookData.BookStatus = "A";
deposit.depositDate = DateTime.Today;
}
dc.SaveChanges();
let me know if that worked for you. 让我知道这是否对您有用。 if not please be specific on what is not working.
如果没有,请具体说明什么不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.