Linq MVC将参数从ActionLink传递到视图以返回记录集
我希望将一个值从链接传递到视图,并在页面上显示单个记录的字段 假设我有Linq MVC将参数从ActionLink传递到视图以返回记录集,linq,asp.net-mvc-5,html.actionlink,request.querystring,Linq,Asp.net Mvc 5,Html.actionlink,Request.querystring,我希望将一个值从链接传递到视图,并在页面上显示单个记录的字段 假设我有@Html.ActionLink(item.Title,“Article/”+@item.ID,“News”)输出/News/Article/1,我需要一个名为NewsController的控制器,具有/News/Article视图 我已有以下资料: 新闻控制器: namespace WebApplication1.Controllers { public class NewsController : Controll
@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
虽然不确定要在视图中放置什么代码,但本文介绍了目前为止我所掌握的内容:
Article.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>
}
@model IEnumerable
@foreach(模型中的var项目){
ViewBag.Title=@item.Title;
@项目.标题
@项目.摘要
@项目.内容
最新消息
@{Html.RenderAction(“最新新闻”,“新闻”);}
}
LatestNews.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>
@{Layout=null;}
@模型IEnumerable
@foreach(模型中的var项目)
{
@Html.MenuLink(item.Title,“Article/”@item.ID,“News”)
}
这适用于Id 1,但需要是动态的
任何帮助都将不胜感激:-)在您的RouteConfig类中,按如下方式映射路线:
routes.MapRoute("Article", "Article/{id}", new {controller = "News", action = "Article"});
public ActionResult Article(int id)
{
var articleModel = (from m in db.News where (m.Id == id) && (m.Active == true) select m);
return View(articleModel);
}
然后在文章方法中,您可以添加并使用id参数,如下所示:
routes.MapRoute("Article", "Article/{id}", new {controller = "News", action = "Article"});
public ActionResult Article(int id)
{
var articleModel = (from m in db.News where (m.Id == id) && (m.Active == true) select m);
return View(articleModel);
}
是否有任何方法可以使用更为seo友好的url结构将/News/Article/1屏蔽为/News/Hero Hour或/News/Article/2屏蔽为/News/Friendly Fire?您是否使用IIS7或更高版本?文章列表是否也相当固定?如果是这样,您可以使用URL重写工具。这不是我自己做过的,但下面的文章看起来很容易理解:我正在通过Visual Studio 2013在IIS Express上开发,不确定远程托管环境中使用了什么IIS。如果性能不是问题,则可以更改文章方法以接受文章名而不是Id,然后根据名称查询数据库。虽然会比较慢(即使有索引),可能不是最好的解决方案,但目前我所能想到的一切。性能应该始终是一个重要因素,尽管数据库目前很小,但仍希望保持它的效率,谢谢。