如何在 ASP.NET MVC 中创建填充集合和 returns 局部视图的服务器方法?
How to create server method, that populates collection and returns partial view, in ASP.NET MVC?
我试图在我的 _Layout.cshtml 文件中调用 IEnumerable 方法。在决赛中我被建议 "use html.action - to call server method that populates collection and returns partial view".
目前我已经创建了部分文件 _Dodatki.cshtml,其中包含对 IEnumerable 方法的调用(Aktualnosci.cs 是模型文件):
@model IEnumerable<DluzynaSzkola.Models.Aktualnosci>
在我的 _Layout.cshtml 中,我使用构造函数调用方法:
@Html.Action("_Dodatki", "AktualnosciController ", new {area="" })
最后我想在我的 AktualnosciConstructor.cs 文件中创建方法。目前我有方法:
[ChildActionOnly]
[ActionName("_Dodatki")]
public ActionResult Dodatki()
{
IList<Aktualnosci> lista = new IList<Aktualnosci>();
return PartialView("_Dodatki", lista);
}
不幸的是,当使用上述语法时,它在编译器中给我消息:
"cannot create an instance of the abstract class or interface
'IList'".
将 'IList' 替换为 'List' 时,出现异常:
"System.Web.HttpException: The controller for path '/' was not found
or does not implement IController."
我不知道如何以其他方式在方法中填充集合。
编辑:根据要求,在AktualnosciController.cs定义之下,没有其他方法:
namespace DluzynaSzkola.Controllers
{
public class AktualnosciController : Controller
{
//here are other methods
[ChildActionOnly]
[ActionName("_Dodatki")]
public ActionResult Dodatki()
{
IList<Aktualnosci> lista = new IList<Aktualnosci>();
return PartialView("_Dodatki", lista);
}
}
}
正如 GTown-Coder 注意到的那样,您的控制器名称似乎有误。相应地更新了我的答案。
我认为您的问题可能与 this SO post 的回答相同。
尝试指定区域名称,如果此控制器不在区域中,只需添加一个空区域名称。
@Html.Action("_Dodatki", "AktualnosciController ", new {area="" })
即使这不能解决您的问题,这也是一种很好的做法,因为如果稍后在某个区域内使用此视图,它将尝试在该区域而不是在根目录中找到控制器 space。
好的,我已经对我的项目进行了更改,效果很好。
我在_Layout.cshtml的调用有点变了。 AktualnosciController 应该只是 Aktualnosci !!!
<div class="kontenerDodatkiLayout hidden-xs hidden-sm">
<div class="archiwum">Archiwum</div>
@Html.Action("_Dodatki", "Aktualnosci", new { area = "" })
</div>
我的部分观点_Dodatki.cshtml模型调用有点变化:
@model IEnumerable<DateTime>
<div class="wpisDodatki">
@foreach (var item in Model)
{
<div> @Html.DisplayFor(modelItem => item)</div>
}
<p>test<br />test<br />test</p>
</div>
我的控制器中的方法 AktualnosciController.cs 看起来像这样:
//[ChildActionOnly]
[ActionName("_Dodatki")]
public ActionResult Dodatki()
{
using (var context = new DluzynaContext())
{
var lista = context.Indeks.Select(it => it.Dzien).ToList();
return PartialView("_Dodatki", lista);
}
}
在这里 lista 被传递到我的部分视图 _Dodatki,并且填充了上下文 属性 Indeks 和模型属性 Dzien.
感谢大家的帮助@Wndrr,@GTown-Coder。
我试图在我的 _Layout.cshtml 文件中调用 IEnumerable 方法。在决赛中我被建议 "use html.action - to call server method that populates collection and returns partial view".
目前我已经创建了部分文件 _Dodatki.cshtml,其中包含对 IEnumerable 方法的调用(Aktualnosci.cs 是模型文件):
@model IEnumerable<DluzynaSzkola.Models.Aktualnosci>
在我的 _Layout.cshtml 中,我使用构造函数调用方法:
@Html.Action("_Dodatki", "AktualnosciController ", new {area="" })
最后我想在我的 AktualnosciConstructor.cs 文件中创建方法。目前我有方法:
[ChildActionOnly]
[ActionName("_Dodatki")]
public ActionResult Dodatki()
{
IList<Aktualnosci> lista = new IList<Aktualnosci>();
return PartialView("_Dodatki", lista);
}
不幸的是,当使用上述语法时,它在编译器中给我消息:
"cannot create an instance of the abstract class or interface 'IList'".
将 'IList' 替换为 'List' 时,出现异常:
"System.Web.HttpException: The controller for path '/' was not found or does not implement IController."
我不知道如何以其他方式在方法中填充集合。
编辑:根据要求,在AktualnosciController.cs定义之下,没有其他方法:
namespace DluzynaSzkola.Controllers
{
public class AktualnosciController : Controller
{
//here are other methods
[ChildActionOnly]
[ActionName("_Dodatki")]
public ActionResult Dodatki()
{
IList<Aktualnosci> lista = new IList<Aktualnosci>();
return PartialView("_Dodatki", lista);
}
}
}
正如 GTown-Coder 注意到的那样,您的控制器名称似乎有误。相应地更新了我的答案。
我认为您的问题可能与 this SO post 的回答相同。
尝试指定区域名称,如果此控制器不在区域中,只需添加一个空区域名称。
@Html.Action("_Dodatki", "AktualnosciController ", new {area="" })
即使这不能解决您的问题,这也是一种很好的做法,因为如果稍后在某个区域内使用此视图,它将尝试在该区域而不是在根目录中找到控制器 space。
好的,我已经对我的项目进行了更改,效果很好。
我在_Layout.cshtml的调用有点变了。 AktualnosciController 应该只是 Aktualnosci !!!
<div class="kontenerDodatkiLayout hidden-xs hidden-sm">
<div class="archiwum">Archiwum</div>
@Html.Action("_Dodatki", "Aktualnosci", new { area = "" })
</div>
我的部分观点_Dodatki.cshtml模型调用有点变化:
@model IEnumerable<DateTime>
<div class="wpisDodatki">
@foreach (var item in Model)
{
<div> @Html.DisplayFor(modelItem => item)</div>
}
<p>test<br />test<br />test</p>
</div>
我的控制器中的方法 AktualnosciController.cs 看起来像这样:
//[ChildActionOnly]
[ActionName("_Dodatki")]
public ActionResult Dodatki()
{
using (var context = new DluzynaContext())
{
var lista = context.Indeks.Select(it => it.Dzien).ToList();
return PartialView("_Dodatki", lista);
}
}
在这里 lista 被传递到我的部分视图 _Dodatki,并且填充了上下文 属性 Indeks 和模型属性 Dzien.
感谢大家的帮助@Wndrr,@GTown-Coder。