【发布时间】:2014-05-06 09:00:34
【问题描述】:
我想将链接中的值传递给视图并在页面上显示单个记录的字段。
假设我有 @Html.ActionLink(item.Title, "Article/"+@item.ID, "News"),它输出 /News/Article/1,我需要一个名为 NewsController 的控制器和 /News/Article 视图。
我已经有以下:
新闻控制器:
namespace WebApplication1.Controllers
{
public class NewsController : Controller
{
private WebApplication1Entities db = new WebApplication1Entities();
public ActionResult Article()
{
var articleModel = (from m in db.News where (m.Id == 1) && (m.Active == true) select m);
return View(articleModel);
}
[ChildActionOnly]
public ActionResult LatestNews()
{
var latestModel = (from m in db.News where (m.Id != 1) && (m.Active == true) select m);
return View(latestModel);
}
}
不确定我是否应该使用 FirstOrDefault(),因为它只能是一条记录,因为 Id 是唯一的,但不确定如何在没有 IEnumerable 列表的情况下引用视图中的项目对象。目前 Id 设置为 1,但我希望记录集反映传递的 Id。
不确定要在视图中放入什么代码,但文章是我目前所拥有的:
文章.cshtml
@model IEnumerable<WebApplication1.Models.News>
@foreach (var item in Model) {
ViewBag.Title = @item.Title;
<div class="row">
<article class="span9 maxheight">
<section class="block-indent-1 divider-bot-2">
<h2>@item.Title</h2>
@item.Summary
@item.Content
</section>
</article>
<article class="span3 divider-left maxheight">
<section class="block-indent-1">
<h2>Latest News</h2>
@{ Html.RenderAction("LatestNews", "News"); }
</section>
</article>
</div>
}
最新消息.cshtml
@{ Layout = null; }
@model IEnumerable<Shedtember.Models.News>
<ul class="nav sf-menu clearfix">
@foreach (var item in Model)
{
@Html.MenuLink(item.Title, "Article/"@item.ID, "News")
}
</ul>
这适用于 Id 1,但这需要是动态的。
任何帮助将不胜感激:-)
【问题讨论】:
标签: linq asp.net-mvc-5 html.actionlink request.querystring