【问题标题】:How to display read more in mvc asp.net如何在 mvc asp.net 中显示更多内容
【发布时间】:2016-08-05 19:52:55
【问题描述】:

在网站的首页,我的数据库中排名前 5 的新闻文章按日期排序,在 100 个字符后显示“...阅读更多”。哪个有效。 我如何在文章的简短描述后出现“阅读更多”的任何文章添加“阅读更多”???

这是我的控制器:

using CBA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CBA.ViewModels;

namespace CBA.Controllers
{
    public class EventController : Controller
    {
        private ModelEntities db = new ModelEntities();

        public ActionResult Index()
        {
            EventDetailsViewModel eventDetail = new EventDetailsViewModel();
            MasterEvents events = new MasterEvents();

            eventDetail.Title = events.Title;
            eventDetail.CreatedTime = events.CreatedTime;
            eventDetail.Detail = events.DetailEvent;
            eventDetail.CreatedBy = events.CreatedBy_Id;
            eventDetail.Description = events.ShortDescription;

            return View(db.MasterEvents.ToList());
        }


        public ActionResult View(int id)
        {
            MasterEvents MasterEvents = db.MasterEvents.Find(id);

            return View();
        }



    }
}

这是我的看法:

@model IEnumerable<CBA.Models.MasterEvents>
@{
    ViewBag.Title = "Recruitment - NA Events";
    ViewBag.lnkEvents = "active";
    Layout = "~/Views/Shared/ContentFrontEnd.cshtml";
}


@Html.Raw(ViewBag.Carousel)
@Html.Raw(ViewBag.Content)

<!DOCTYPE html>

<html>
<head>

    <title>Index</title>
</head>
<body>


    <div class="container" style="background-color: white; border-radius: 0 0 15px 15px; margin-bottom: 40px;">
        <div class="row" style="margin-top: 30px;">
            <div class="col-lg-10 col-lg-offset-1">
                @foreach (var item in Model)
                {
                    <div class="blog-post">
                        <h2 class="blog-post-title">@Html.DisplayFor(modelItem => item.Title)</h2>
                        <p class="blog-post-meta">Created Time @Html.DisplayFor(modelItem => item.CreatedTime) <!--by <a href="#">@Html.DisplayFor(modelItem => item.CreatedBy_Id)</a></p>-->
                        <h2 class="blog-post-title">@Html.DisplayFor(modelItem => item.ShortDescription)</h2>
                        <div class="readmore">
                            <p>
                            @Html.DisplayFor(modelItem => item.DetailEvent)
                        </p>
                             </div>
                        <p class="blog-post-meta">Update Time @Html.DisplayFor(modelItem => item.CreatedTime)
                        <p>


                            <br />

                            <input id="Button2" type="button" value="Read More..." class="btn btn-default" />
                        </p>
                    </div>   
                }


                <nav>
                    <ul class="pager">
                        <li><a href="#">Previous</a></li>
                        <li><a href="#">Next</a></li>
                    </ul>
                </nav>
            </div>
        </div>
    </div>
</body>
</html>

这是我的模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web;

namespace CBA.Models
{
    public class MasterEvents
    {
        [Key]
        [DisplayName("ID")]
        [ScaffoldColumn(false)]
        public int Id { get; set; }

        [Required(ErrorMessage = "Required Events Title")]
        [DisplayName("Title")]
        [StringLength(250)]
        public string Title { get; set; }

        [Required(ErrorMessage = "Required Short Description")]
        [DisplayName("Short Description")]
        [StringLength(250)]
        public string ShortDescription { get; set; }

        [Required(ErrorMessage = "Required Details")]
        [DisplayName("Detail Events")]
        public string DetailEvent { get; set; }

        [DisplayName("Created Time")]
        public System.DateTime? CreatedTime { get; set; }

        [DisplayName("Updated Time")]
        public System.DateTime? UpdatedTime { get; set; }

        [DisplayName("Created By")]
        public int? CreatedBy_Id { get; set; }

        [DisplayName("Updated By")]
        public int? UpdatedBy_Id { get; set; }
    }
}

【问题讨论】:

  • 您为控制器和视图提供了相同的代码。无论如何,为什么不在 Javascript 中实现这个技巧呢?
  • 问题令人困惑

标签: c# asp.net-mvc hyperlink


【解决方案1】:

将每个项目实例封装在一个表单中,这样当用户点击阅读更多输入时,将发布包含文章 Id 的隐藏文本框,然后您可以在控制器中检索整篇文章然后显示它在视图中。

            @foreach (var item in Model)
            {
              @using(Html.BeginForm("View", "Event")){
                <div class="blog-post">
                    <h2 class="blog-post-title">@Html.DisplayFor(modelItem => item.Title)</h2>
                    <p class="blog-post-meta">Created Time @Html.DisplayFor(modelItem => item.CreatedTime) <!--by <a href="#">@Html.DisplayFor(modelItem => item.CreatedBy_Id)</a></p>-->
                    <h2 class="blog-post-title">@Html.DisplayFor(modelItem => item.ShortDescription)</h2>
                    <div class="readmore">
                        <p>
                        @Html.DisplayFor(modelItem => item.DetailEvent)
                    </p>
                         </div>
                    <p class="blog-post-meta">Update Time @Html.DisplayFor(modelItem => item.CreatedTime)
                    <p>


                        <br />
                        @Html.HiddenFor(m => item.Id)
                        <input id="Button2" type="button" value="Read More..." class="btn btn-default" />
                    </p>
                </div>   
              }
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    相关资源
    最近更新 更多