To insert Data into Multiple Tables using MVC ASP.NET
Asked Answered
G

2

5

I am new to MVC ASP.NET. Although this might have been solved in the past but I still can't get a complete Solution to my problem.

Problem : I have two tables
Video

Columns are : VideoID, FlimName, Description

VideoDetails

Columns are : VideoDetailsID , Actor , VideoID (foreign key from table video)

I am looking to insert data into both these tables from my MVC Model.[ first insertion into Video Table , followed by VideoDetails with the primary key of Video Table ie VideoID also getting inserted along with the record of VideoDetails Table ].

I have found some solutions to the problem , but it doesn't seem to merge into one complete solution.

Can I get some useful links on a step By step approach to this problem OR can someone help me with this solution.

I have read about the ViewModel approach on this forum but not clear still about its functionality and the flow. Please help.

Thanks,

Mangesh

Gaylegayleen answered 26/2, 2013 at 12:35 Comment(0)
C
6

The method you want is to use Entity Framework. You can find more information about Entity Framework here:

http://www.asp.net/entity-framework

If you are using Entity Framework, the code will look something like this:

var context = new MyDBModelContext();

tblVideo video = new tblVideo()
{
    FileName = theFileName,
    Description = theDescription
};

context.tblVideos.Add(video);
context.SaveChanges();

tblVideoDetails details = new tblVideoDetails()
{
    VideoID = video.VideoID,
    Actor = theActor,
};

context.tblVideoDetails.Add(details);
context.SaveChanges();

If you are using SQL, pass the respective details into your Stored Procedure and insert as follows:

INSERT INTO Video
    (FileName, Description)
    VALUES(@FileName, @Description)

INSERT INTO VideoDetails
    (VideoID, Actor)
    VALUES(SCOPE_IDENTITY( ), @Actor)

NOTE: The above SQL should work, test first though.

Comedian answered 26/2, 2013 at 12:56 Comment(2)
i do not intend to use an SP for this. I want to use Models which will map with my database columns . I have read about 'View Model' approach on this forum , but not clear. help Required.Gaylegayleen
@MangeshKaslikar That's good news. Don't forget to mark the answer you found work as 'Correct' on Stack Overflow. That way it is useful for everybody.Comedian
A
3

There are different solution available to perform this. If you wanto to use knockout then exampel is here. You can also see this post

You may see another way of doing it here

You may also see Mvc Controls Toolkit

Aideaidedecamp answered 26/2, 2013 at 12:43 Comment(2)
do you know how to solve it with 'ViewModels' . I read couple of threads on this forum about it but still not clear .Gaylegayleen
Domain model may span to multiple views or a singel view may contain data from multiple domain models. View models are classes that are mapped back to domain model. you can find multiple resources on internet regarding this you may see this weblogs.asp.net/shijuvarghese/archive/2010/02/01/…Aideaidedecamp

© 2022 - 2024 — McMap. All rights reserved.