I'm making Library Management System project in which I have a Deposit
Controller which deposits the earlier issued books. Two tables are used:
BooksData
has all book info with a BookStatus
Flag which sets 'A' for Available and 'I' for Issued. BookDepositIssue
has BookID
which is common in both tables and IssueDate, DepositDate, and DueDate
.
Now in my deposit controller I'm checking if the BookStatus
flag is "I"; which means Issued. And also DueDate<= DateTime
.Today so that no fine is incurred. 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. The database has issued books whose flag is "I" then why it is not executing the condition?
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
. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.