0

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...

1
  • Do not add a 'Mode` property, just check the value of the Id property. 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 is Role role = _m2Sctx.Roles.Where(s => s.RoleID == someValue); return View(role); Commented Oct 30, 2014 at 1:24

3 Answers 3

1

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");
        }
Sign up to request clarification or add additional context in comments.

2 Comments

its giving me an error on add: 'System.Data.Linq.Table<M2DAL.M2Service.Role>' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Data.Linq.Table<M2DAL.M2Service.Role>' could be found (are you missing a using directive or an assembly reference?)
Have udpated code, Use InsertOnSubmit instead of Add.
0

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;
        }

Comments

0
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.

2 Comments

its giving me an error on ad: Error 6 'System.Data.Linq.Table<M2DAL.M2Service.Role>' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Data.Linq.Table<M2DAL.M2Service.Role>' could be found (are you missing a using directive or an assembly reference?)
Sorry, I gave you the code for EF. From your error, I'm assuming you're using Linq to Sql? I updated the answer if that's the case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.