[英]MVC 4 Passing Data To Partial View - Format
I'm having trouble getting my head around partial views in MVC4. 我无法理解MVC4中的部分视图。 I currently have a User Profile page and I want to have a partial view displaying each record from another table that contains their UserID.
我目前有一个“用户配置文件”页面,我希望有一个局部视图来显示另一个包含其UserID的表中的每个记录。
This is the HTML helper I'm using to call my function in the controller . 这是我用来在控制器中调用函数的HTML帮助器。
@Html.Action("DisplayArticles", "Articles")
This is the method I call in my Article controller to display a user's articles. 这是我在“文章”控制器中调用的用于显示用户文章的方法。
[HttpGet]
[ChildActionOnly]
public ActionResult DisplayArticles()
{
int id = WebSecurity.CurrentUserId;
var articleList = new List<Article>();
//Article articles = (from j in db.Article
// where j.UserID == id
// select j).ToList();
//articleList.AddRange(articles);
foreach (Article i in db.Article)
{
if (i.UserID == id)
{
articleList.Add(i);
}
}
return PartialView("_DisplayWritersArticle", articleList);
}
My partial view _DisplayWriterArticle simply uses HTML helpers to display the data. 我的局部视图_DisplayWriterArticle仅使用HTML帮助器来显示数据。
@model Writegeist.Models.Article
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.UserID)
</th>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Type)
</th>
<th>
@Html.DisplayNameFor(model => model.Content)
</th>
</tr>
<tr>
<th>
@Html.DisplayFor(model => model.UserID)
</th>
<td>
@Html.DisplayFor(model => model.Title)
</td>
<td>
@Html.DisplayFor(model => model.Type)
</td>
<td>
@Html.DisplayFor(model => model.Content)
</td>
</tr>
</table>
My problem is the way that I'm passing my list into the view, It isn't getting recognised and I'm getting the error 我的问题是我将列表传递到视图中的方式,它没有被识别并且出现了错误
> The model item passed into the dictionary is of type
> 'System.Collections.Generic.List`1[Writegeist.Models.Article]', but
> this dictionary requires a model item of type
> 'Writegeist.Models.Article'.
If I change 如果我改变
return PartialView("_DisplayWritersArticle", articleList);
to 至
return PartialView("_DisplayWritersArticle", new Writegeist.Models.Article());
I assume the articleList isn't in the right format. 我认为articleList的格式不正确。 Can anyone point me in the right direction?
谁能指出我正确的方向? Thanks
谢谢
Your Partial View is expecting a single Article, you're giving it a list of them. 您的“部分视图”期望只有一篇文章,您将为其提供清单。
Change the model to a list of Articles: 将模型更改为文章列表:
@model List<Writegeist.Models.Article>
Then you have to loop through the list to display them all: 然后,您必须遍历列表以显示所有内容:
<table>
@foreach(Article article in Model) {
<tr>
<th>
@Html.DisplayNameFor(a => article.UserID)
</th>
<th>
@Html.DisplayNameFor(a => article.Title)
</th>
<th>
@Html.DisplayNameFor(a => article.Type)
</th>
<th>
@Html.DisplayNameFor(a => article.Content)
</th>
</tr>
}
</table>
The problem is as I see it that you are passing a List but you are telling the view it is a just an Article. 问题是,正如我看到的那样,您正在传递列表,但是您告诉视图它只是一篇文章。
Change your 改变你的
@model Writegeist.Models.Article to @model List<Writegeist.Models.Article>
Then you'll want to iterate over that list to get the data you are expecting. 然后,您将需要遍历该列表以获取所需的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.