Business ERP Part 45 Edit Stock Categories in ASP.NET MVC
Hi, Dear's here we learn how to implement Auto Time Table Generator in Visual Studio using C# Windows Form. Ilyasoft software company provide full project step by step training on our YouTube Channel ilyasoft software company so now subscribe, share and like for more project base tutorials
In this part we are going to edit stock categories in tblCategory in database, so now follow below code.
Source Code:
Action C# Code :
public ActionResult EditCategory(int? categoryID)
{
if (string.IsNullOrEmpty(Convert.ToString(Session["UserName"])))
{
return RedirectToAction("Login", "Home");
}
var userid = 0;
var usertypeid = 0;
var companyid = 0;
var branchid = 0;
var branchtypeid = 0;
int.TryParse(Convert.ToString(Session["UserID"]), out userid);
int.TryParse(Convert.ToString(Session["UserTypeID"]), out usertypeid);
int.TryParse(Convert.ToString(Session["CompanyID"]), out companyid);
int.TryParse(Convert.ToString(Session["BranchID"]), out branchid);
int.TryParse(Convert.ToString(Session["BranchTypeID"]), out branchtypeid);
var category = DB.tblCategories.Find(categoryID);
var categorymv = new CategoryMV();
categorymv.CategoryID = category.CategoryID;
categorymv.BranchID = category.BranchID;
categorymv.categoryName = category.categoryName;
categorymv.CompanyID = category.CompanyID;
categorymv.BranchID = category.BranchID;
return View(categorymv);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditCategory(CategoryMV categoryMV)
{
if (string.IsNullOrEmpty(Convert.ToString(Session["UserName"])))
{
return RedirectToAction("Login", "Home");
}
var userid = 0;
var usertypeid = 0;
var companyid = 0;
var branchid = 0;
var branchtypeid = 0;
int.TryParse(Convert.ToString(Session["UserID"]), out userid);
int.TryParse(Convert.ToString(Session["UserTypeID"]), out usertypeid);
int.TryParse(Convert.ToString(Session["CompanyID"]), out companyid);
int.TryParse(Convert.ToString(Session["BranchID"]), out branchid);
int.TryParse(Convert.ToString(Session["BranchTypeID"]), out branchtypeid);
if (ModelState.IsValid)
{
var checkcategory = DB.tblCategories.Where(u => u.categoryName == categoryMV.categoryName.Trim() && u.CategoryID != categoryMV.CategoryID && u.CompanyID == companyid && u.BranchID == branchid).FirstOrDefault();
if (checkcategory == null)
{
var editcategory = DB.tblCategories.Find(categoryMV.CategoryID);
editcategory.categoryName = categoryMV.categoryName;
editcategory.UserID = userid;
DB.Entry(editcategory).State = System.Data.Entity.EntityState.Modified;
DB.SaveChanges();
return RedirectToAction("AllCategories");
}
else
{
ModelState.AddModelError("categoryName", "Already Exist!");
}
}
return View(categoryMV);
}
View Code :
@model ERP_App.Models.CategoryMV
@{
ViewBag.Title = "Category";
}
<div class="col-lg-6">
<div class="card card-default mb-6">
<div class="card-header">Edit Category</div>
<div class="card-body">
@using (Html.BeginForm("EditCategory", "Stock", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(u => u.CategoryID)
@Html.HiddenFor(u => u.CompanyID)
@Html.HiddenFor(u => u.BranchID)
@Html.HiddenFor(u => u.UserID)
<div class="form-group">
@Html.LabelFor(model => model.categoryName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.categoryName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.categoryName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Update" class="btn btn-outline-success" />
@Html.ActionLink("Back", "AllCategories", null, new { @class = "btn btn-outline-default" })
</div>
</div>
</div>
}
</div>
</div>
</div>
Comments
Post a Comment