locked
How to Save Data to Multiple Table using Single Model (MVC 4) RRS feed

  • Question

  • User-1714886539 posted

    Hi,

    I have a three tables lik..

    1) customer_master 2) customer_address 3) customer_contact

    Customer_Id is a pk in (customer_master)

    Customer_Id is a fk in (customer_address)

    Customer_Id is a fk in (customer_contact)

    I want to save the data in first table (customer_master) then take a auto-generated auto_id (pk) as a reference and save to (customer_address) and (customer_contact) tables.

    Please some body help me, how to create a single model and save data to multiple tables.

    Thanks in advance..

    Monday, June 20, 2016 12:46 PM

Answers

  • User-271186128 posted

    Hi Bhavik Dharaiya,

    The Last one is composite/aggregate model. Is it right way to creating model and controller ?

    Yes, you can create model like that.

    After submit the forum, in the action method, first, you need to insert MB_Customer_Master table and get the auto-generated Auto_Id. Then, insert values into MB_Address_Details and MB_Contact_Details table.

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 22, 2016 7:00 AM
  • User-271186128 posted

    Hi Bhavik Dharaiya,

    You can refer to the following code:

    Code in model:

        public class ProductInfo
        {
            public int ProID { get; set; }
            public string ProName { get; set; }
            public int TypeID { get; set; }
        }
        public class ProductType
        {
            public int TypeID { get; set; }
            public string TypeName { get; set; }
        }
        public class ProductTypeViewModel
        {
            public ProductInfo ProductModel { get; set; }
            public ProductType TypeModel { get; set; }
        }

    Code in controller:

            public ActionResult Index11()
            {
                ProductTypeViewModel vm = new ProductTypeViewModel();
                return View(vm);
            }
            [HttpPost]
            public ActionResult Index11(ProductTypeViewModel vm)
            {
                ProductType type = vm.TypeModel;  
    //insert type //get the typeID
    ProductInfo infor = vm.ProductModel;

    //Insert the Product return View(vm); }

    Code in view:

    @model MVCTestSample.Models.ProductTypeViewModel
    
        @using (Html.BeginForm("Index11","MyTest", FormMethod.Post)) 
        {
            @Html.AntiForgeryToken()
            
            <div class="form-horizontal">
                <h4>ProductTypeViewModel</h4>
                <hr />
                @Html.ValidationSummary(true)
    
                <div class="form-group">
                    @Html.LabelFor(model => model.ProductModel.ProID, new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.ProductModel.ProID)
                        @Html.ValidationMessageFor(model => model.ProductModel.ProID)
                    </div>
                </div>
    
                <div class="form-group">
                    @Html.LabelFor(model => model.ProductModel.ProName, new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.ProductModel.ProName)
                        @Html.ValidationMessageFor(model => model.ProductModel.ProName)
                    </div>
                </div>
    
    
                <div class="form-group">
                    @Html.LabelFor(model => model.TypeModel.TypeID, new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.TypeModel.TypeID)
                        @Html.ValidationMessageFor(model => model.TypeModel.TypeID)
                    </div>
                </div>
    
                <div class="form-group">
                    @Html.LabelFor(model => model.TypeModel.TypeName, new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.TypeModel.TypeName)
                        @Html.ValidationMessageFor(model => model.TypeModel.TypeName)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Create" class="btn btn-default" />
                    </div>
                </div>
            </div>
        }
        
        <div>
            @Html.ActionLink("Back to List", "Index")
        </div>

    More details about the CRUD operation, you can refer to these articles:

    http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application

    http://www.c-sharpcorner.com/UploadFile/2072a9/crud-operations-in-mvc/

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 24, 2016 7:07 AM
  • User-271186128 posted

    Hi Bhavik Dharaiya,

    entity type has no key defined viewmodel

    As the error message said, the entity has no key defined. Check the relevant model and use Key Attribute to define key:

    http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.aspx

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 27, 2016 9:30 AM

All replies

  • User-1678860823 posted

    Hi Bhavik,

    You could do the below:

    1. Create three domain Entities, each for one of the tables Custome_Master, Customer_Address and Customer_Contact.

    2. Create a Model for the view by aggregating all these three entities. Meaning create a Model class with all the required members of the above three classes.

    3. Create a View using the above Model.

    4. When user fills out the View and clicks submits, you could take the data from the Model and then assign it to each of the domain class.

    5. First persist the Customer_Master entity it into the database and collect the ID back once the record is created in the DB(Before persisting your ID in the class is empty).

    6. Fill the ID in the other two entities from step 5 and persist them in the database.

    Hope it helps. 

    Thanks

    Monday, June 20, 2016 4:11 PM
  • User-1714886539 posted

    Hi Rocky,

    Thanks for reply...

    I have used your tricks please, let me know am i right ?

    namespace GymAppDemo.Models
    {
        public class MB_Customer_Master
        {
            [Key]
            public int Auto_Id { get; set; }    //Primary Key
            public string Customer_Code { get; set; }
            public string Customer_Name { get; set; }
            public string Remarks { get; set; }
        }
    }
    namespace GymAppDemo.Models
    {
        public class MB_Address_Details
        {
            [Key]
            public int Auto_Id { get; set; }            //Primary Key
            public int Parent_Master_Id { get; set; }   //Foreign Key
            public string Customer_Code { get; set; }
            public string Record_For { get; set; }
            public string Address { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string Country { get; set; }
            public string Zip_Code { get; set; }
            public string Remarks { get; set; }
        }
    }
    namespace GymAppDemo.Models
    {
        public class MB_Contact_Details
        {
            [Key]
            public int Auto_Id { get; set; }            //Primary Key
            public int Parent_Master_Id { get; set; }   //Foreign Key
            public string Record_For { get; set; }
            public string Contact_Type { get; set; }
            public string Contact_No { get; set; }
            public string Address { get; set; }
         }
    }

    namespace GymAppDemo.Models
    {
        public class Customers
        {

            public  MB_Customer_Master CustMastModel { get; set; }
            public MB_Address_Details AddDetailModel { get; set; }
            public MB_Contact_Details ContDtailModel { get; set; }
        }
    }

    The Last one is composite/aggregate model. Is it right way to creating model and controller ?

    Thanks in advance

    Tuesday, June 21, 2016 11:21 AM
  • User-271186128 posted

    Hi Bhavik Dharaiya,

    The Last one is composite/aggregate model. Is it right way to creating model and controller ?

    Yes, you can create model like that.

    After submit the forum, in the action method, first, you need to insert MB_Customer_Master table and get the auto-generated Auto_Id. Then, insert values into MB_Address_Details and MB_Contact_Details table.

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 22, 2016 7:00 AM
  • User-1714886539 posted

    Thanks for reply...

    Please can you help me how to save or can you share some code ?

    It's a greate appriciate....

    Friday, June 24, 2016 6:14 AM
  • User-271186128 posted

    Hi Bhavik Dharaiya,

    You can refer to the following code:

    Code in model:

        public class ProductInfo
        {
            public int ProID { get; set; }
            public string ProName { get; set; }
            public int TypeID { get; set; }
        }
        public class ProductType
        {
            public int TypeID { get; set; }
            public string TypeName { get; set; }
        }
        public class ProductTypeViewModel
        {
            public ProductInfo ProductModel { get; set; }
            public ProductType TypeModel { get; set; }
        }

    Code in controller:

            public ActionResult Index11()
            {
                ProductTypeViewModel vm = new ProductTypeViewModel();
                return View(vm);
            }
            [HttpPost]
            public ActionResult Index11(ProductTypeViewModel vm)
            {
                ProductType type = vm.TypeModel;  
    //insert type //get the typeID
    ProductInfo infor = vm.ProductModel;

    //Insert the Product return View(vm); }

    Code in view:

    @model MVCTestSample.Models.ProductTypeViewModel
    
        @using (Html.BeginForm("Index11","MyTest", FormMethod.Post)) 
        {
            @Html.AntiForgeryToken()
            
            <div class="form-horizontal">
                <h4>ProductTypeViewModel</h4>
                <hr />
                @Html.ValidationSummary(true)
    
                <div class="form-group">
                    @Html.LabelFor(model => model.ProductModel.ProID, new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.ProductModel.ProID)
                        @Html.ValidationMessageFor(model => model.ProductModel.ProID)
                    </div>
                </div>
    
                <div class="form-group">
                    @Html.LabelFor(model => model.ProductModel.ProName, new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.ProductModel.ProName)
                        @Html.ValidationMessageFor(model => model.ProductModel.ProName)
                    </div>
                </div>
    
    
                <div class="form-group">
                    @Html.LabelFor(model => model.TypeModel.TypeID, new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.TypeModel.TypeID)
                        @Html.ValidationMessageFor(model => model.TypeModel.TypeID)
                    </div>
                </div>
    
                <div class="form-group">
                    @Html.LabelFor(model => model.TypeModel.TypeName, new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.TypeModel.TypeName)
                        @Html.ValidationMessageFor(model => model.TypeModel.TypeName)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Create" class="btn btn-default" />
                    </div>
                </div>
            </div>
        }
        
        <div>
            @Html.ActionLink("Back to List", "Index")
        </div>

    More details about the CRUD operation, you can refer to these articles:

    http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application

    http://www.c-sharpcorner.com/UploadFile/2072a9/crud-operations-in-mvc/

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 24, 2016 7:07 AM
  • User-1714886539 posted

    Dear Friend,

    Thanks you very much for reply and share the code with me...

    I am getting errors like "entity type has no key defined viewmodel" when i am going to creating controller using entity framework.

    Friday, June 24, 2016 1:22 PM
  • User-271186128 posted

    Hi Bhavik Dharaiya,

    entity type has no key defined viewmodel

    As the error message said, the entity has no key defined. Check the relevant model and use Key Attribute to define key:

    http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.aspx

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 27, 2016 9:30 AM