views:

109

answers:

1

Hi i am having problem inserting data into multiple table from one view. First understand the scenario: I have a student table and a student name table. Name table contains Firstname, Middlename, Lastname. Student table contains a foreign key of name table Id. I made a data model by ADO.net. Now how can i pass both Student and Name data to the Create student View?

//StudentsController: Create View [GET Method]

    public ActionResult Create()
    {
        return View();
    }

//Create View

Inherits="System.Web.Mvc.ViewPage<DomainModel.Models.Student>"

+1  A: 

You could either a) create a strongly type class that encompasses both the student and student name table

class StudentViewData
{
  Domain.Models.StudentName Student { get; set;}
  IEnumerable<StudentTable> Students { get; set;}
}

and pass this in to your view

or b) use

ViewData["StudentTable"] = DomainModel.Models.StudentTable

and in the view

foreach(var s in (DomainModel.Models.StudentTable )ViewData["StudentTable"]) ...

me personally I would use a)

I am making a couple of assumptions of your model from your explanation.

Rippo
i also think a) is a good idea. But as a beginner it is quite difficult to me. Can you show me a little elaborately?
Mr. Flint
I would need to see more of your code to help more.
Rippo
I have just started my code. It was the beginning. Student Table Contains student iformation. Id, Name, address, dob etc. Name and Address are multivalued attributes. So i defined two tables for them "StudentName" and "StudentAddress" and put the foreign key reference of these tables in Student table; Now if i want to add a new student, i have to add StudentName and StudentAddress first. I hope you can understand. Now i want to create a create form where student can enter information. And the entered information will be save din different tables that means StudnetName, StudentAddress, Student.
Mr. Flint
Please help me out. I need some materials so that i can learn how multi table(related) insertion is done...
Mr. Flint
I suggest learning more about MVC. see http://www.asp.net/mvc/ and click the "Learn" link. I am not sure still about your data structure. IF you are using Datasets/Datatables then I suggest you learn about how to populate the strongly typed classes from this.Sorry but I cannot be of more help.
Rippo