将模型传递给 Razor 视图(部分等)
Passing models to Razor views (partials, etc.)
如何在 C# MVC 中的 partial/views/controllers 之间传输 data/model?
我正在尝试复制 this 示例,但是 @model IEnumerable<MVC_BasicTutorials.Models.Student>
给我一个错误,Model
下有红线
_StudentList Partial.cshtml
@model IEnumerable<MVC_BasicTutorials.Models.Student>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.StudentName)
</th>
<th>
@Html.DisplayNameFor(model => model.Age)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.StudentName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Age)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.StudentId }) |
@Html.ActionLink("Details", "Details", new { id=item.StudentId }) |
@Html.ActionLink("Delete", "Delete", new { id = item.StudentId })
</td>
</tr>
}
</table>
StudentController
:
public class StudentController : Controller
{
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public int Age { get; set; }
}
IList<Student> students;
public StudentController()
{
students = new List<Student>{
new Student() { StudentId = 1, StudentName = "John", Age = 18 } ,
new Student() { StudentId = 2, StudentName = "Steve", Age = 21 } ,
new Student() { StudentId = 3, StudentName = "Bill", Age = 25 } ,
new Student() { StudentId = 4, StudentName = "Ram" , Age = 20 } ,
new Student() { StudentId = 5, StudentName = "Ron" , Age = 31 } ,
new Student() { StudentId = 6, StudentName = "Chris", Age = 17 } ,
new Student() { StudentId = 7, StudentName = "Rob",Age = 19 } ,
};
}
// GET: Student
public ActionResult Index()
{
return View(students);
}
}
Index.cshtml
:
@model IEnumerable<MVC_BasicTutorials.Models.Student>
<h3>Student List</h3>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@{
Html.RenderPartial("_StudentList", Model);
}
我读了这个 但无法解决它 :( 如果我 运行 应用程序,我在 @foreach (var item in Model)
处收到错误,即 NullReferenceException: Object reference not set to an instance of an object.
您的 Student
class 被定义为 StudentController
的子 class。因此,在使用此 class 作为模型类型时,您需要使用正确的命名空间/完全限定的 class 名称。
将第一行改为
@model IEnumerable<PutYourNameSpaceHere.StudentController.Student>
您需要将PutYourNameSpaceHere
替换为您StudentController
下的命名空间class是
或
将 Student
class 移动到新文件(甚至现有文件),但在命名空间 MVC_BasicTutorials.Models
下
namespace MVC_BasicTutorials.Models
{
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public int Age { get; set; }
}
}
执行此操作时,您需要确保在 StudentController
中有一个 using 语句,以便您可以使用此 class
因此将此添加为 StudentController
class
的第一行
@using MVC_BasicTutorials.Models
如何在 C# MVC 中的 partial/views/controllers 之间传输 data/model?
我正在尝试复制 this 示例,但是 @model IEnumerable<MVC_BasicTutorials.Models.Student>
给我一个错误,Model
_StudentList Partial.cshtml
@model IEnumerable<MVC_BasicTutorials.Models.Student>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.StudentName)
</th>
<th>
@Html.DisplayNameFor(model => model.Age)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.StudentName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Age)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.StudentId }) |
@Html.ActionLink("Details", "Details", new { id=item.StudentId }) |
@Html.ActionLink("Delete", "Delete", new { id = item.StudentId })
</td>
</tr>
}
</table>
StudentController
:
public class StudentController : Controller
{
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public int Age { get; set; }
}
IList<Student> students;
public StudentController()
{
students = new List<Student>{
new Student() { StudentId = 1, StudentName = "John", Age = 18 } ,
new Student() { StudentId = 2, StudentName = "Steve", Age = 21 } ,
new Student() { StudentId = 3, StudentName = "Bill", Age = 25 } ,
new Student() { StudentId = 4, StudentName = "Ram" , Age = 20 } ,
new Student() { StudentId = 5, StudentName = "Ron" , Age = 31 } ,
new Student() { StudentId = 6, StudentName = "Chris", Age = 17 } ,
new Student() { StudentId = 7, StudentName = "Rob",Age = 19 } ,
};
}
// GET: Student
public ActionResult Index()
{
return View(students);
}
}
Index.cshtml
:
@model IEnumerable<MVC_BasicTutorials.Models.Student>
<h3>Student List</h3>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@{
Html.RenderPartial("_StudentList", Model);
}
我读了这个 但无法解决它 :( 如果我 运行 应用程序,我在 @foreach (var item in Model)
处收到错误,即 NullReferenceException: Object reference not set to an instance of an object.
您的 Student
class 被定义为 StudentController
的子 class。因此,在使用此 class 作为模型类型时,您需要使用正确的命名空间/完全限定的 class 名称。
将第一行改为
@model IEnumerable<PutYourNameSpaceHere.StudentController.Student>
您需要将PutYourNameSpaceHere
替换为您StudentController
下的命名空间class是
或
将 Student
class 移动到新文件(甚至现有文件),但在命名空间 MVC_BasicTutorials.Models
namespace MVC_BasicTutorials.Models
{
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public int Age { get; set; }
}
}
执行此操作时,您需要确保在 StudentController
中有一个 using 语句,以便您可以使用此 class
因此将此添加为 StudentController
class
@using MVC_BasicTutorials.Models