分配给href标签

时间:2019-05-06 14:50:40

标签: asp.net asp.net-mvc

我试图将gridview列设置为链接列,但是当从视图模型传递值时,它们不显示。我不确定哪里出了问题。请帮我解决这个问题。谢谢!

ViewModel

 public class ViewModelBase
 {
        //Current user data
        public string Id { get; set; }
        public string UserName { get; set; }
        public string Name { get; set; }

        //Report data
        public string ReportName { get; set; }
        public string Brand { get; set; }
        public string FileName { get; set; }
        public string FilePath { get; set; }

        public virtual List<Report> Reports { get; set; }
}
public class ReportViewModel : ViewModelBase
{  
}
public class CurrentUserViewModel : ViewModelBase
{
}

用户报告:

public class ReportRepository
{
        public List<Report> CurrentUserReports()
        {
            FulfillmentContext db = new FulfillmentContext();
            var userRepository = new UserRepository();
            var currentUser = userRepository.GetCurrentUser();
            var allReports = db.Reports;

            return (from r in allReports.ToList()
                    where (r.UserId == currentUser.Id)
                    orderby r.CreatedOn descending
                    select r).ToList();                        
        }
}    

控制器:

public ActionResult Index()
{
            var reportRepository = new ReportRepository();
            var userRepository = new UserRepository();

            var userReports = reportRepository.CurrentUserReports();
            var currentUser = userRepository.GetCurrentUser();

            var viewModel = new ViewModelBase
            {
                Reports = userReports,
                Name = currentUser.Name,
                UserName = currentUser.UserName
            };

            return View(viewModel);
}

查看:

@model FulfillmentPortal.ViewModels.ViewModelBase
@{
    ViewBag.Title = "Home Page";
    Layout = "~/Views/Shared/_Layout.cshtml";
    var grid = new WebGrid(Model.Reports, rowsPerPage: 20, canPage: true, canSort: true);
}

<script>pageindicator = 0;</script>

<div>
    <div class="sc-home-splash"></div>

    <div class="panel panel-primary">
        <div class="panel-heading">REPORTS</div>
        <div class="panel-body" style="padding-left: 35px;">
            @if (Model.Reports.Count() != 0)
            {
                <div id="grid">
                    @grid.GetHtml(tableStyle: "grid-table",
                    headerStyle: "grid-header",
                    footerStyle: "grid-footer",
                    alternatingRowStyle: "grid-alter-row",
                    rowStyle: "grid-row",
                    mode: WebGridPagerModes.All,
                    firstText: "<< First",
                    previousText: "< Prev",
                    nextText: "Next >",
                    lastText: "Last >>",
                    columns: grid.Columns(
                    grid.Column("ReportName", "Report Name", format: @<a href="~/Content/Report/@Model.FileName">@Model.ReportName</a>),
                    grid.Column("Brand", "Brand"),
                    grid.Column("FileName", "File Name"),
                    grid.Column("CreatedOn", "Date")

                    ))
                </div>
            }
            else
            {
                <p>No Records Found!</p>
            }

        </div>
    </div>

在这一点上,我正在使用以下格式:@ Model.FileName和@ Model.ReportName模型值是空的。请帮助我找出问题所在。

1 个答案:

答案 0 :(得分:0)

您忘记在Index函数中设置ViewModel的这两个属性。您仅设置报告,名称和用户名。 设置这两个