I'm learning MVC and I'm trying to render a Partialview on Home Page. I'm getting following error :
我正在学习MVC,我正在尝试在主页上呈现部分视图。我收到以下错误:
"HttpException"
My very simple model is as follows:
我非常简单的模型如下:
public class simpleModel
{
public int id { get; set; }
public string Name { get; set; }
}
Controller:
public ActionResult ShowNames()
{
UsersContext db = new UsersContext();
return PartialView(db.SimpleModels.ToList());
}
partial view:
@model List<MvcApplication1.Models.simpleModel>
@if (Model != null)
{
<ul>
@foreach (var item in Model)
{
<li>@item.Name</li>
}
</ul>
}
Home page where I want my partial view to render is
我想要部分视图渲染的主页是
@{
ViewBag.Title = "Home Page";
}
@section featured {
<section class="featured">
<p>To learn more about ASP.NET MVC visit </p>
</section>
}
@{
Html.RenderAction("ShowNames", "Simple");
}
3 个解决方案
#1
Your ShowNames()
method returns a collection of simpleModel
items to the view (as in db.SimpleModels.ToList()
) but your partial declares the model as a single simpleModel
onject. That would throw and exception, as would your attempt to iterate a single object using the foreach
loop.
您的ShowNames()方法将simpleModel项的集合返回给视图(如在db.SimpleModels.ToList()中),但您的partial将模型声明为单个simpleModel onject。这会抛出异常,就像你尝试使用foreach循环迭代单个对象一样。
Change the partial view model declaration to
将局部视图模型声明更改为
@model List<MvcApplication1.Models.simpleModel>
but since <li>name</li>
would just return the text "name" (or assuming its actually <li>@name</li>
then it would return the text "MvcApplication1.Models.simpleModel" unless you have overridden the ToString()
method of simpleModel
), so you need to access a property of the object
但由于
<ul>
@foreach (var item in model)
{
<li>@item.Name</li>
}
</ul>
#2
Your model might be null i.e its not having any data. So, you should put a check before the foreachloop
您的模型可能为空,即没有任何数据。所以,你应该在foreachloop之前检查一下
@model List
@if(model != null)
{
<ul>
@foreach (var item in model)
{
<li>@item.Name</li>
}
</ul>
}
#3
Try adding [ChildActionOnly] attribute to your ShowNames method.
尝试将[ChildActionOnly]属性添加到ShowNames方法。
#1
Your ShowNames()
method returns a collection of simpleModel
items to the view (as in db.SimpleModels.ToList()
) but your partial declares the model as a single simpleModel
onject. That would throw and exception, as would your attempt to iterate a single object using the foreach
loop.
您的ShowNames()方法将simpleModel项的集合返回给视图(如在db.SimpleModels.ToList()中),但您的partial将模型声明为单个simpleModel onject。这会抛出异常,就像你尝试使用foreach循环迭代单个对象一样。
Change the partial view model declaration to
将局部视图模型声明更改为
@model List<MvcApplication1.Models.simpleModel>
but since <li>name</li>
would just return the text "name" (or assuming its actually <li>@name</li>
then it would return the text "MvcApplication1.Models.simpleModel" unless you have overridden the ToString()
method of simpleModel
), so you need to access a property of the object
但由于
<ul>
@foreach (var item in model)
{
<li>@item.Name</li>
}
</ul>
#2
Your model might be null i.e its not having any data. So, you should put a check before the foreachloop
您的模型可能为空,即没有任何数据。所以,你应该在foreachloop之前检查一下
@model List
@if(model != null)
{
<ul>
@foreach (var item in model)
{
<li>@item.Name</li>
}
</ul>
}
#3
Try adding [ChildActionOnly] attribute to your ShowNames method.
尝试将[ChildActionOnly]属性添加到ShowNames方法。