【问题标题】:MVC Pass Paramater from ActionLink to View to return RecordsetMVC 将参数从 Action Link 传递到 View 以返回 Recordset
【发布时间】: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


    【解决方案1】:

    在您的 RouteConfig 类中映射如下路由:

    routes.MapRoute("Article", "Article/{id}", new {controller = "News", action = "Article"});
    

    然后在您的 Article 方法中,您可以添加和使用 id 参数,如下所示:

        public ActionResult Article(int id)
        {
            var articleModel = (from m in db.News where (m.Id == id) && (m.Active == true) select m);
            return View(articleModel);
        }
    

    【讨论】:

    • 有没有办法使用更友好的 url 结构将 /News/Article/1 屏蔽为 /News/Hero-Hour 或 /News/Article/2 为 /News/Friendly-Fire?
    • 您使用的是 IIS7 或更高版本吗?文章列表也相当固定吗?如果是这样,您可以使用 URL 重写工具。不是我自己做的,但下面的文章看起来很容易理解:Creating Rewrite Rules for the URL Rewrite Module
    • 我正在通过 Visual Studio 2013 在 IIS Express 上进行开发,不确定远程托管环境中使用的是什么 IIS。
    • 如果性能不是问题,那么您可以更改 Article 方法以接受文章名称而不是 Id,然后根据名称查询数据库。虽然会慢一些(即使有索引),而且可能不是最好的解决方案,但我现在能想到的。
    • 性能应该始终是一个重要因素,尽管目前数据库很小,但仍希望保持高效,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    • 2019-06-15
    • 1970-01-01
    • 2013-12-10
    相关资源
    最近更新 更多