Reputation: 646
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...
Upvotes: 0
Views: 154
Reputation: 1733
var newRole = new Role {
RoleName = roleForm.RoleName,
RoleDescription = roleForm.RoleDescription
};
Srvctx.Roles.InsertOnSubmit(newRole);
Assuming that your "Role" entity class is called Role
. Also, you can populate the CustomerBlacklist as well if its part of your "Role" class.
Upvotes: 0
Reputation: 3015
Try bellow code :
if (ModelState.IsValid)
{
if (roleForm.RoleId == 0)
{
var role = new Role()
{
RoleName = roleForm.RoleName,
RoleDescription = roleForm.RoleDescription
};
Srvctx.Roles.InsertOnSubmit(role);
Srvctx.SubmitChanges();
}
else
{
var role = Srvctx.Roles.FirstOrDefault(w => w.RoleID == roleForm.RoleId);
role.RoleName = roleForm.RoleName;
role.RoleDescription = roleForm.RoleDescription;
Srvctx.SubmitChanges();
}
return RedirectToAction("RoleManagement");
}
Upvotes: 1
Reputation: 3015
For edit model instead of
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;
}
Create one method that return single object of Role based on roleId :
public M2Portal.Areas.Admin.Models.Users.Roles Roles(int roleId)
{
var roleDBEntity = _m2Sctx.Roles.FirstOrDefault(s => s.RoleID == RoleId);
var roleModelEntity = new M2Portal.Areas.Admin.Models.Users.Roles(){
RoleId = roleDBEntity.RoleId,
RoleName = roleDBEntity.RoleName,
RoleDescription = roleDBEntity.RoleDescriptio
CustomerBlacklist = roleDBENtity.CustomerBlacklist // Assuming there's relation between Role and CustomerBlackList table
};
return roleModelEntity;
}
Upvotes: 0