Edit table data using auto mapper - c#

Hi i am building a web application for school.
I am trying to update student information, i already did teacher part
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Save(Teacher teacher)
{
if (!ModelState.IsValid)
{
var data = teacher;
return View("TeacherForm", data);
}
if (teacher.Id == 0)
_context.Teachers.Add(teacher);
else
{
var dataInDb = _context.Teachers.Single(c => c.Id == teacher.Id);
dataInDb.Name = teacher.Name;
dataInDb.Designation = teacher.Designation;
dataInDb.EducationalQualification = teacher.EducationalQualification;
dataInDb.DateOfBirth = teacher.DateOfBirth;
dataInDb.PhoneNumber = teacher.PhoneNumber;
dataInDb.StartDate = teacher.StartDate;
dataInDb.EndDate = teacher.EndDate;
dataInDb.Status = teacher.Status;
}
_context.SaveChanges();
return RedirectToAction("Index", "Teacher");
}
But in Student part i want to use auto mapper for map data
if (student.Id == 0)
_context.Students.Add(student);
else
{
var dataInDb = _context.Students.Single(c => c.Id == student.Id);
Mapper.Map(student, dataInDb);
}
but its not working. I try to edit data but the table data remain same.
its relay difficult to write every property for student.
how can i solve this problem?

Related

C# - Retrieve data from persistence storage and save it to the view model

Hello I have a controller method that I want to return the view model of that looks like this
This is what it would look like if it was hard-coded
public ActionResult SpecialOrderSummary(int? id)
{
// Retrieve data from persistence storage and save it to the view model.
// But here I am just faking it.
var vm = new ItemViewModel
{
ItemId = 123,
ItemName = "Fake Item",
Parts = new List<ItemPartViewModel>
{
new ItemPartViewModel
{
PartId = 1,
PartName = "Part 1"
},
new ItemPartViewModel
{
PartId = 2,
PartName = "Part 2"
}
}
};
return View(vm);
}
But I obviously don't want it hard coded. So this is what I was trying to do instead to achieve my goal
public ActionResult SpecialOrderSummary(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
JobOrder jobOrder = db.JobOrders.Find(id);
if (jobOrder == null)
{
return HttpNotFound();
}
ViewBag.JobOrderID = jobOrder.ID;
ItemInstance ii = db.ItemInstances.Where(x => x.serialNumber == jobOrder.serialNumber).FirstOrDefault();
Item item = db.Items.Find(ii.ItemID);
var vm = new ItemViewModel
{
ItemId = item.ItemID,
ItemName = item.Name,
Parts = new List<ItemPartViewModel>
{
foreach(ItemHasParts ihp in item.IHP)
{
Part part = db.Parts.Find(ihp.PartID);
new ItemPartViewModel
{
PartId = part.ID,
PartName = part.Name
};
}
}
};
return View(vm);
}
But that doesn't work. As it doesn't seem to recognize the closing }
of the opening "Parts" and the opening "vm" bracket as it skips both. Why is this?
Hmmm I thought I answered this question before: https://stackoverflow.com/a/62782124/2410655. Basically you can't have a for loop like that in the middle of the view model.
I would like to add 2 more things to it.
1. Id?
If the special order summary expects an ID, don't declare it as optional. If you do so, you have to add more logic to check whether there is an ID or not.
If the order summary expects an ID, just declare it as int id. And if the client doesn't provide it, let the MVC framework handle the error. Now depending on your setup, your MVC might throw a 404, or 500, or a user-friendly page. It's up to you, the developer, to set it up.
2. Be careful on NullReference Exception
In your code example, I see you used FirstOrDefault() on the item instance. That will bite you if it comes back as NULL and you call db.Items.Find(ii.ItemID)...
So based on your example, I would change the code to:
public ActionResult SpecialOrderSummary(int id)
{
JObOrder jobOrder = db.JobOrders.Find(id);
if (jobOrder == null)
{
return HttpNotFound();
}
ItemInstance itemInstance = db.ItemInstances
.Where(x => x.serialNumber == jobOrder.serialNumber)
.FirstOrDefault();
Item item = null;
if (itemInstance != null)
{
item = db.Items.Find(itemInstance.ItemID);
}
var vm = new JobOrderSummaryViewModel
{
JobOrderId = jobOrder.ID,
Parts = new List<ItemPartViewModel>();
};
if (item != null)
{
vm.ItemId = item.ItemId;
vm.ItemName = item.ItemName;
foreach(ItemHasParts ihp in item.IHP)
{
// Does Part and Item have many-to-many relationships?
// If so, you might be able to get the part by
// ihp.Part instead of looking it up using the ID.
// Again, it depends on your setup.
Part part = db.Parts.Find(ihp.PartID);
if (part != null)
{
vm.Parts.Add(new ItemPartViewModel
{
PartId = part.ID,
PartName = part.Name
});
}
}
}
return View(vm);
}
Note:
You have additional calls back to the database inside the loop (db.Parts.Find(ihp.PartID);). That will cause performance issue if you have huge data. Is there any way you can fetch all your data you needed once at the beginning?

If ID exists, delete before insert to avoid duplication (C# / ASP.NET)

I am using the code below to insert records into a SQL Server table. The code works perfectly, but the part I need to add is a check to make sure that the ID passed does not exist before inserting it. If records with the passed ID exists, those records need to be deleted, and then inserted again.
Would it be possible to ask for help to figure out the part that would check the ID before the insert?
I will continue to try to figure it out on my own, but I hope someone could offer some help if possible.
Here is my code:
public ActionResult InsertData(List<Meeting> meeting)
{
bool status = false;
if (ModelState.IsValid)
{
using (MeetingEntities db = new MeetingEntities())
{
foreach (var i in meeting)
{
db.Meeting.Add(i);
}
db.SaveChanges();
status = true;
}
}
return new JsonResult { Data = new { status = status } };
}
Thank you,
Erasmo
Check it against our meeting list before adding to the context object like
using (MeetingEntities db = new MeetingEntities())
{
foreach (var i in meeting)
{
if(!meeting.Any(x => x.ID == i.ID)) {
db.Meeting.Add(i);
}
}
db.SaveChanges();
status = true;
}
You said * I need to check and if exists delete those records with that MeetingId* .. then you can do something like below
var meetingIds = meeting.Select(x => x.ID).ToList();
db.Meeting.Where(x => meetingIds.Contains(x.ID))
.ToList().ForEach(db.Meeting.DeleteObject);
db.SaveChanges();
Well you can combine of this operations
using (MeetingEntities db = new MeetingEntities())
{
//insert operation
foreach (var i in meeting)
{
if(!meeting.Any(x => x.ID == i.ID)) {
db.Meeting.Add(i);
}
}
//Delete Operation
var meetingIds = meeting.Select(x => x.ID).ToList();
db.Meeting.Where(x => meetingIds.Contains(x.ID))
.ToList().ForEach(db.Meeting.DeleteObject);
// Save the changes
db.SaveChanges();
status = true;
}
maybe try to check if is already present if not insert it.. like:
public ActionResult InsertData(List<Meeting> meeting)
{
bool status = false;
if (ModelState.IsValid)
{
using (MeetingEntities db = new MeetingEntities())
{
foreach (var i in meeting)
{
if(db.Meeting.FirstOrDefault(xx=>xx. ID == i. ID) == null)
{
db.Meeting.Add(i);
}
}
db.SaveChanges();
status = true;
}
}
return new JsonResult { Data = new { status = status } };
}

How do I Filter a table view by using TWO tables in MVC Controller?

I'm struggling with Filtering my table view based on TWO table attributes. I can display them fine using data from a single table, but once I add a filter from a second table and the Identity it won't display. Im trying "show results using TABLE 1 where Table1 ID == Table2 ID AND Table 2 User == User.identity.name"... in case it makes a difference... Table 1 is "Safes", Table 2 is a bridge table of "Users and Safes".
Any Help much appreciated :)
public ActionResult GetSafe(Users_and_Safe uas)
{
using (CBREntities2 dc = new CBREntities2())
{
var safe = dc.Safes.Where(a => a.Safe_ID == uas.Safe_ID && uas.User_ID == User.Identity.Name).Select(s => new { Safe_ID = s.Safe_ID, Department_ID = s.Department_ID, User_ID = s.User_ID }).ToList();
return Json(new { data = safe }, JsonRequestBehavior.AllowGet);
}
}
So I have found the answer and thought I would post for anyone in the future who has the issue:
public ActionResult GetSafeHome(Users_and_Safe uas)
{
using (CBREntities2 dc = new CBREntities2())
{
var allUAS = dc.Users_and_Safes.Where(b => b.User_ID == User.Identity.Name).Select(c => c.Safe_ID).ToList();
var homeSafes = dc.Safes.Where(x => (allUAS.Contains(x.Safe_ID))).Select(s => new { Safe_ID = s.Safe_ID, Department_ID = s.Department_ID }).ToList();
return Json(new { data = homeSafes }, JsonRequestBehavior.AllowGet);
}
}

Enter new data in db (EF)

I have method in controller
It receive data from post request and write to table
Here is code
[ResponseType(typeof(TimeTable))]
public IHttpActionResult PostTimeTable(TimeTable timeTable)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (ModelState.IsValid)
{
DateTime dt = DateTime.Today;
TimeTable c = (from x in db.TimeTables
where x.Company == timeTable.Company && x.INN == timeTable.INN
select x).First();
c.StartPause = timeTable.StartPause;
c.StartDay = timeTable.StartDay;
c.EndPause = timeTable.EndPause;
c.EndDay = timeTable.EndDay;
db.SaveChanges();
}
db.TimeTables.Add(timeTable);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = timeTable.Id }, timeTable);
}
But it works well when record with INN and Company already in db.
But if it not in database I need to create new entry.
How I need to modify this method?
You can use a flag (exisingCompanyFlag) for edit mode or add new mode like this
bool existingCompanyFlag = true;
TimeTable c = (from x in db.TimeTables
where x.Company == timeTable.Company && x.INN == timeTable.INN
select x).FirstOrDefult();
if (c == null)
{
existingCompanyFlag = false;
c = new TimeTable();
}
c.StartPause = timeTable.StartPause;
c.StartDay = timeTable.StartDay;
c.EndPause = timeTable.EndPause;
c.EndDay = timeTable.EndDay;
if (!existingCompanyFlag)
db.TimeTables.Add(c);
You need a separate branch in your code for the insert case.
if (ModelState.IsValid) {
if (addingNewRow) {
TimeTable tt = new TimeTable {
// Populate properties (except identity columns)
};
db.TimeTables.Add(tt);
} else {
// update
}
db.SaveChanges();
}
To link to other entities use one of:
Assign instances:
x.Company = theCompany;
or, assign the instance id
x.CompanyId = companyId;
(#1 is easier if you already have the other entity loaded or are creating it – EF will sort out the ids – while #2 saves loading the whole other entity.)

Edit and delete a specific view model in a list

I am making a shopping cart and for keeping track of the cart I have a session which contains a list of product view models.
This is the action method for adding to cart:
public ActionResult AddToCart(string id)
{
List<CartVM> cartVMList = new List<CartVM>();
CartVM cartVM = new CartVM();
int productId = Int32.Parse(id);
Db db = new Db();
var result = db.Products.FirstOrDefault(x => x.Id == productId);
decimal price = result.Price;
cartVM.ProductId = productId;
cartVM.Quantity = 1;
cartVM.Price = price;
if (Session["cart"] != null)
{
cartVMList = (List<CartVM>)Session["cart"];
cartVMList.Add(cartVM);
}
else
{
cartVMList.Add(cartVM);
}
Session["cart"] = cartVMList;
//return Content(id);
return RedirectToAction("Index", "ShoppingCart");
}
It works when adding new products, so e.g. if I add 5 new products the session will contain a list of 5 products, but how do I edit and delete a specific view model from the list, based on for example the ProductId ?
I haven't tested it, but the following should work. All you need to do is grab the cart list like you did when adding to cart. Instead of adding a new item, you just edit the object in the list or remove it from the list.
Technically, unless Session does something special, you shouldn't need to re-save the list to the Session if you got it from the session, since a list is a reference type.
public ActionResult EditCartItem(string id, int quantity, decimal price)
{
if (Session["cart"] != null)
{
var cartVMList = (List<CartVM>) Session["cart"];
var itemToEdit = cartVMList.FirstOrDefault(cartVM => cartVM.Id == id);
if(itemToEdit == null)
return this.HttpNotFound();
itemToEdit.Quantity = quantity;
itemToEdit.Price = price;
}
}
public ActionResult RemoveFromCart(string id)
{
if (Session["cart"] != null)
{
var cartVMList = (List<CartVM>) Session["cart"];
var itemToRemove = cartVMList.FirstOrDefault(cartVM => cartVM.Id == id);
if(itemToRemove == null)
return this.HttpNotFound();
cartVMList.Remove(itemToRemove);
}
}

Categories