Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi I want to do update delete Process using Code First approach and view model in mvc4.I had create the view and i done insertion process using Code First approach and ViewModel because my DB is fully normalized and i used EF .
so I used code first approach and ViewModel. Now I complete the Insertion process . Now I want to know how to do Update and Delete process using Code First approach and ViewModel. Please any one give me the solution..


My Model

public class CustomerModel1
    {
        public System.Guid CustomerID { get; set; }
        public string DisplayName { get; set; }
        public string PrintName { get; set; }
 }

public partial class ContactModel
    {
        public System.Guid ContactID { get; set; }
        public string DisplayName { get; set; }
        public string PrintName { get; set; }
        public string Mobile1 { get; set; }
        public string Mobile2 { get; set; }
        public string Phone1 { get; set; }
        public string Phone2 { get; set; }
        public string Email1 { get; set; }
         public string Website1 { get; set; }
}




My VieModel
public class CustomerViewModel
{
  public System.Guid CustomerID { get; set; }
    public string CustomerName { get; set; }
    public System.Guid ContactID { get; set; }
    public string ContactPerson { get; set; }
    public string PhoneNo1 { get; set; }
    public string PhoneNo2 { get; set; }
    public string MobileNo1 { get; set; }
    public string MobileNo2 { get; set; }
    public string Website1 { get; set; }
 }
  public class VisitorsEntities1 : DbContext
{

 public DbSet Customer    { get; set; }
 public DbSet Contact { get; set; }
}



My Controller
public ActionResult Create()
    {
  return View();

    }

    [HttpPost]
 public ActionResult Create(CustomerViewModel viewmodel)

  {
     var Customerobj = new Customer()
        { 
         CustomerID= Guid .NewGuid (),
         DisplayName = viewmodel.CustomerName,
         PrintName = viewmodel .CustomerName
   };
    var Contactobj = new Contact()
        {
            ContactID= Guid.NewGuid(),
            DisplayName= viewmodel.CustomerName,
            PrintName=viewmodel.CustomerName,
            Mobile1=viewmodel.MobileNo1,
            Mobile2 = viewmodel.MobileNo2,
            Phone1= viewmodel.PhoneNo1,
            Phone2=viewmodel.PhoneNo2,
            Email1=viewmodel.Email1,
            Website1=viewmodel.Website1

            db.Customer.Add(Customerobj);
            db.Contact.Add(Contactobj);
            db.SaveChanges();
       return View();
   }


Here i complete the insert process using CodeFirstApproach and ViewModel. Insertion is working fine. Now I want to do Update ,Delete and Details (Index) Process . How to do that using code first approach and ViewModel.

Thanks...
Posted
Updated 15-Dec-15 6:05am
v3
Comments
Nathan Minier 15-Dec-15 7:20am    
Way too generic, please explain what problem you're having, what you've tried, and post the code blocks that are giving you problems.
Member 12087373 15-Dec-15 7:55am    
ok wait i will share my code
Nathan Minier 15-Dec-15 13:06pm    
Is there a reason that the normal delete and update actions aren't working?
For instance:

public ActionResult DeleteCustomer(Guid customerId)
{
var toDelete = db.Customers.Find(customerId);
var deleted = false;

if(toDelete != null)
{
db.Entry(toDelete).State = EntityState.Deleted;
deleted = db.SaveChages() > 0;
}
return View(deleted);
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900