Asp.net MVC从一个视图到两个模型 [英] Asp.net MVC from one view to two models
问题描述
大家好,
我是关于MVC的新手。我正在从这里学习它。但我对模型和视图有疑问。
现在我的数据库中有2个表。
学生
ID ---姓名---姓氏---课程
课程
ID-课程
我在创建视图中有一个表单:
名称 - 姓氏 - 课程
现在我可以使用创建视图添加到学生表中的学生。但是我想同时在我的课程表中添加课程文本。
这是我的控制器:
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 :
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);
}
我的尝试:
我搜索了codeproject,但找不到任何相关内容。
What I have tried:
I searched codeproject about this but couldn't find anything about it.
推荐答案
你只能向视图发送一个模型。
因此,创建另一个视图模型类,其中包含两个表的数据属性,每个属性都有一个属性。
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.
这篇关于Asp.net MVC从一个视图到两个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!