How to update specific field not all using Core
I am calling getting data using below code.
public IActionResult Edit(int? Id)
{
try
{
var edititem = _context.ItemMasterFiles.Find(Id);
if (edititem != null)
{
var item = new ItemMasterFile();
{
item.CodeItem = edititem.CodeItem;
item.Descriptionitem = edititem.Descriptionitem;
item.SecId = edititem.SecId;
item.Cid = edititem.Cid;
item.Weight = edititem.Weight;
item.BaleSize = edititem.BaleSize;
};
var sectionList = (from s in _context.Sections
select new SelectListItem()
{
Text = s.Secnam,
Value = s.SecId.ToString()
}).ToList();
sectionList.Insert(0, new SelectListItem()
{
Text = "----Select----",
Value = string.Empty
});
item.Listofsections = sectionList;
var categoryList = (from s in _context.Catagories
select new SelectListItem()
{
Text = s.Cname,
Value = s.Cid.ToString()
}).ToList();
categoryList.Insert(0, new SelectListItem()
{
Text = "----Select----",
Value = string.Empty
});
item.Listofcategory = categoryList;
return View(item);
}
else
{
TempData["errorMessage"] = "Item Detail is not available..";
return RedirectToAction("ItemList");
}
}
catch (Exception ex)
{
TempData["errorMessage"] = ex.Message;
return RedirectToAction("ItemList");
}
}
And updating specific field as below code
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(ItemMasterFile item)
{
if (ModelState.IsValid)
{
var CheckitemName = _context.ItemMasterFiles.Where(u => u.Descriptionitem == u.Descriptionitem && u.CodeItem != u.CodeItem).FirstOrDefault();
if (CheckitemName == null)
{
var edititem = new ItemMasterFile()
{
CodeItem = item.CodeItem,
Descriptionitem = item.Descriptionitem,
Weight = item.Weight,
BaleSize = item.BaleSize
};
_context.Entry(edititem).State = EntityState.Modified;
_context.SaveChanges();
return RedirectToAction("ItemList");
}
}
else
{
ModelState.AddModelError("Descriptionitem", "Already Exist");
}
return View();
}
But it is update all fields, which should not be.
CodeItem = item.CodeItem,
Descriptionitem = item.Descriptionitem,
Weight = item.Weight,
BaleSize = item.BaleSize