Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I'm a newbie about MVC. And i'm learning it from here. But i have a question about models and view.
Now i have 2 tables in my db.

Students
ID---Name---Surname---Course

Courses
ID-Courses

And i have a form in my Create view :

Name - Surname - Course

Now i can add with Create view to Students table a student. But i want to add course text in to my Courses table at the same time.

Here is my controller :


C#
public ActionResult Create([Bind(Include = "student_id,name,surname,course")] student student)
     {
         if (ModelState.IsValid)
         {

             db.student.Add(student);
             db.SaveChanges();
             return RedirectToAction("Index");
         }

         return View(student);
     }


What I have tried:

I searched codeproject about this but couldn't find anything about it.
Posted
Updated 1-Aug-16 9:36am

1 solution

You can only send ONE model to a view.

So, create another view model class that includes properties for the data for both tables, one property for each.
 
Share this answer
 
Comments
kozmikadam 1-Aug-16 16:47pm    
Hello Dave,
I'm trying to understand , now in my db i have 2 table as i said. And all models created automatically after i connect with my db.

For example i want to add same value like course in two table with one form. You said create another MVC , that includes course data twice ?

Sorry , i'm really a newbie about it.
Dave Kreskowiak 1-Aug-16 17:03pm    
No, I said create another view model class. One that holds all of the data you need to pass to the view in order for the view to show everything it needs to and possibly everything it needs to interact with the user.

It's not entirely clear what you're doing with this view. What does this view do?
kozmikadam 2-Aug-16 0:38am    
In this view i have just a form for add students to my "student" table. Also i'm trying to add course name to a second table which name is "course". So in this view i have just a form.

Is there any article about this type work ?

I'm trying to a very simple thing i think. With the classic way ( asp.net web application ) i can do this without any problem. But in MVC i couldn't solve yet.
F-ES Sitecore 2-Aug-16 4:12am    
google "view with two models" and you'll find loads of examples. Create you're own model and populate yourself

class MyViewModel
{
List<Customer> Customers {get;set;}
List<Order> Orders {get;set;}
}

MyViewModel model = new MyViewModel()
model.Customers = dbcontext.Customers().ToList();
model.Orders = dbcontext.Orders().ToList();
return View(model);
Dave Kreskowiak 2-Aug-16 10:14am    
The correct method for this would be to add a student and then redirect to another view for editing the student where you can add courses for that student.

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