am trying to add some data using a form to the database.
I already have few data and managed to get the edit working, but to add am having abit of problem.
This is my edit code to edit the data in the database:
[HttpPost]
public ActionResult Save(M2Portal.Areas.Admin.Models.Users.Roles roleForm)
{
try
{
if (ModelState.IsValid)
{
if (Mode == "Add")
{
****This is where my add code goes*******
}
else
{
var role = Srvctx.Roles.FirstOrDefault(w => w.RoleID == roleForm.RoleId);
role.RoleName = roleForm.RoleName;
role.RoleDescription = roleForm.RoleDescription;
Srvctx.SubmitChanges();
}
return RedirectToAction("RoleManagement");
}
return RedirectToAction("RoleManagement");
}
catch (Exception e)
{
return RedirectToAction("RoleManagement");
}
}
this is the code for the model:
This is for the add.
public Roles()
{
Mode = "Add";
RoleId = 0;
RoleDescription = "";
RoleName = "";
CustomerBlacklist = new List<vw_RoleCustomerBlacklist>();
}
This is for the edit: which works.
public Roles(int roleId)
{
Mode = "Edit";
//RoleId = roleId;
RoleId = roleId;
RoleName = _m2Sctx.Roles.Where(s => s.RoleID == RoleId).Select(c => c.RoleName).FirstOrDefault();
RoleDescription = _m2Sctx.Roles.Where(s => s.RoleID == RoleId).Select(c => c.RoleDescription).FirstOrDefault();
CustomerBlacklist = _m2Sctx.vw_RoleCustomerBlacklists.Where(s => s.RoleId == roleId).ToList();
CustName = CustName;
}
So now am working with the add functionality, which am having problems with.
to add new data in the form... any ideas:
if (Mode == "Add")
{
****This is where my add code goes*******
}
you can see my edit how that is set up, but for add its completely different, and there is where am fining it hard...
Idproperty. If its zero, then its new and should be added, otherwise its existing. And do not that all that db logic in your constructor. All you need isRole role = _m2Sctx.Roles.Where(s => s.RoleID == someValue); return View(role);