[英]Create.cshtml on the index.cshtml page?
I have renderad a CRUD with .net and have the following files in the same folder. 我用.net渲染了CRUD,并在同一文件夹中包含以下文件。
Create.cshtml, Delete.cshtml, Details.cshtml, Edit.cshtml, Index.cshtml Create.cshtml,Delete.cshtml,Details.cshtml,Edit.cshtml,Index.cshtml
Now it is a link to create a new record on the index page 现在,它是在索引页面上创建新记录的链接
@Html.ActionLink("Create New", "Create")
But I want the create form to be on the index page. 但是我希望创建表单在索引页面上。
I tested to use PartialView (I do not know whether it is better to copy the code from create.cshtml in index.cshtml or to use two files but I get the same error). 我测试过使用PartialView(我不知道从index.cshtml中的create.cshtml复制代码还是使用两个文件是更好的选择,但是我遇到相同的错误)。
Changed the return
in the controller to return PartialView(board);
改变
return
在控制器中return PartialView(board);
And the view to @Html.Partial("Create")
并查看到
@Html.Partial("Create")
But I get the compiling error 但是我得到了编译错误
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Klotterplank.Models.Board]', but this dictionary requires a model item of type 'Klotterplank.Models.Board'.
传递到词典中的模型项的类型为'System.Collections.Generic.List`1 [Klotterplank.Models.Board]',但是此词典需要类型为'Klotterplank.Models.Board'的模型项。
This is the model I use in index.cshtml 这是我在index.cshtml中使用的模型
@model IEnumerable<Klotterplank.Models.Board>
And this is the model I use in create.cshtml 这是我在create.cshtml中使用的模型
@model Klotterplank.Models.Board
如果您在具有不同模型的父页面中使用PartialView,则必须将其他模型传递给Html Helper,如下所示:
@Html.Partial("Create", new Klotterplank.Models.Board())
The error tells pretty much itself: When a partial view is instantiated, it gets a copy of the parent view's ViewData dictionary . 该错误几乎可以说明一切:实例化部分视图时,它会获取父视图的ViewData dictionary的副本 。 Your
Create
view needs data of different type, so you can provide it like this: 您的“
Create
视图需要不同类型的数据,因此您可以像这样提供数据:
@Html.Partial("Create", viewModel)
viewModel
can be equal to Model.Fist()
(since on your Index
view you have a collection of boards as model) for example. viewModel
可以等于Model.Fist()
(因为在Index
视图上,您有一组木板作为模型)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.