ACP
ACP

Reputation: 35268

asp.net mvc insert doesnt seem to work for me

My controller's call to repository insert method all the values are passed but it doesn't get inserted in my table..

My controller method,

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "Id")]FormCollection collection)
        {
            try
            {
                MaterialsObj materialsObj = new MaterialsObj();
                materialsObj.Mat_Name = collection["Mat_Name"];
                materialsObj.Mes_Id = Convert.ToInt64(collection["MeasurementType"]);
                materialsObj.Mes_Name = collection["Mat_Type"];
                materialsObj.CreatedDate = System.DateTime.Now;
                materialsObj.CreatedBy = Convert.ToInt64(1);
                materialsObj.IsDeleted = Convert.ToInt64(1);
                consRepository.createMaterials(materialsObj);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

and my repository,

public MaterialsObj createMaterials(MaterialsObj materialsObj)
{
    Material mat = new Material();
    mat.Mat_Name = materialsObj.Mat_Name;
    mat.Mat_Type = materialsObj.Mes_Name;
    mat.MeasurementTypeId = materialsObj.Mes_Id;
    mat.Created_Date = materialsObj.CreatedDate;
    mat.Created_By = materialsObj.CreatedBy;
    mat.Is_Deleted = materialsObj.IsDeleted;
    db.Materials.InsertOnSubmit(mat);
    return materialsObj;
}

What am i missing here any suggestion....

Upvotes: 1

Views: 557

Answers (3)

bruno conde
bruno conde

Reputation: 48265

Aren't you missing a call to SubmitChanges?

db.Materials.InsertOnSubmit(mat);
db.SubmitChanges();

Upvotes: 1

Fabian
Fabian

Reputation: 13691

db.Materials.InsertOnSubmit(mat);
db.SubmitChanges(); // <---------------
return materialsObj;

Upvotes: 1

Chris
Chris

Reputation: 6638

As far as I can see, you're missing a db.SubmitChanges(); to save to the database.

Upvotes: 2

Related Questions