【问题标题】:Passing models to Razor views (partials, etc.)将模型传递给 Razor 视图(部分等)
【发布时间】:2017-02-16 21:59:27
【问题描述】:

如何在 C# MVC 中的部分/视图/控制器之间传输数据/模型?

我正在尝试复制 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);
}

我阅读了这个https://stackoverflow.com/a/11326470/2441637,但无法解决它:(如果我运行应用程序,我会在@foreach (var item in Model) 收到错误,即NullReferenceException: Object reference not set to an instance of an object.

【问题讨论】:

  • MVC_BasicTutorials.Models命名空间下是否有一个名为Student的类?
  • 根本没有MVC_BasicTutorials.Models,不知道应该在哪里以及如何
  • 因为学生类在你的学生控制器中..路径可能是{SolutionName}.Controllers.StudentController.Student
  • 完整的 pah 不起作用,它只有在我使用 [myExistingNameSpace].Model 创建了一个单独的类后才起作用,非常感谢您的反馈。

标签: c# asp.net-mvc razor partial-views


【解决方案1】:

您的Student 类被定义为StudentController 的子类。所以在使用这个类作为模型类型时需要使用正确的命名空间/全限定类名。

将第一行改为

@model IEnumerable<PutYourNameSpaceHere.StudentController.Student>

您需要将PutYourNameSpaceHere 替换为StudentController 类下的命名空间

或者

Student 类移动到一个新文件(甚至是现有文件),但在命名空间 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 语句,以便您可以使用此类

所以将其添加为StudentController 类的第一行

@using MVC_BasicTutorials.Models

【讨论】:

  • Full pah 不起作用,它只有在我用[myExistingNameSpace].Model 创建一个单独的类并且不需要添加@using MVC_BasicTutorials.Models 才能运行后才起作用,但它在VS所以我添加了它。感谢您的支持,如果您根据我的评论稍微调整您的答案,我将很乐意接受并投票。再次感谢
猜你喜欢
  • 2020-04-24
  • 1970-01-01
  • 2021-12-01
  • 2012-06-13
  • 2019-02-01
  • 1970-01-01
  • 2012-02-02
  • 1970-01-01
相关资源
最近更新 更多