MVC-5:显示全名

时间:2015-04-05 04:19:46

标签: c# asp.net-mvc razor

我想从视图顶部的计算属性中显示FullName。当我查看Autos日志时,我可以看到FullName属性。但是Html助手中使用的lambda表达式只显示属性名称,而不是数据。有没有办法显示日志似乎显示的数据?

更新:

完整视图:

@model IEnumerable<SlamJammersData.Model.Attendances>

@{
    ViewBag.Title = "Rosters";
}

<div class="container container-fluid">
    <div class="panel panel-info">
        <div class="panel-body">
            <div class="container">


                @using (Html.BeginForm("Index", "Attendances", FormMethod.Get))
                {

                    <div class="row">
                        <div class="col-md-4">
                            <p>
                                <h3>Attendances for player: @Html.DisplayNameFor(model => model.FullName) </h3>
                            </p>
                        </div>
                        <div class="col-md-4">
                            <p>
                                <h4>Attendances Recorded: @Model.Count() </h4>
                            </p>
                            </div>

                        </div>
                }

            </div>
        </div>
            </div>
        </div>

        <div class="container">
            <div class="row">
                <div class="col-md-2">
                    <p>
                        @Html.DisplayNameFor(model => model.Type)
                    </p>
                </div>

                <div class="col-md-2">
                    <p>
                        @Html.DisplayNameFor(model => model.AttendanceDate)
                    </p>
                </div>

                <div class="col-md-2">
                    <p>
                        @Html.DisplayNameFor(model => model.Comments)
                    </p>
                </div>
            </div>
           @foreach (var item in Model)
           {
               <div class="row table-striped">
                   <div class="col-md-2">
                       <p>
                           @Html.DisplayFor(modelItem => item.Type)
                       </p>
                   </div>

                   <div class="col-md-2">
                       <p>
                           @Html.DisplayFor(modelItem => item.AttendanceDate)
                       </p>
                   </div>

                   <div class="col-md-2">
                       <p>
                           @Html.DisplayFor(modelItem => item.Comments)
                       </p>
                   </div>

                   <div class="col-md-1">
                       <p>
                           <input class="btn btn-xs btn-info" type="button" value="Details" onclick=" location.href = '@Url.Action("Details", "Attendances", new {id = item.Id}, null)' "/>
                       </p>
                   </div>

                   <div class="col-md-1">
                       <p>
                           <input class="btn btn-xs btn-info" type="button" value="Edit" onclick=" location.href = '@Url.Action("Edit", "Attendances", new {id = item.Id}, null)' "/>
                       </p>
                   </div>
                   <div class="col-md-2">
                       <p>
                           <input class="btn btn-xs btn-success" type="button" value="Add New Attendance" onclick=" location.href = '@Url.Action("Create", "Attendances", new {enrollments_Attendance = item.Enrollments_Attendance}, null)' "/>
                       </p>
                   </div>
                   <div class="col-md-1">
                       <p>
                           <input class="btn btn-xs btn-danger" type="button" value="Delete" onclick=" location.href='@Url.Action("Delete", "Attendances", new {id = item.Id}, null)' " />
                       </p>
                   </div>
               </div>


           }

        </div>

控制器:

public ActionResult Index(int? Id )
{
    //Get the rosters and their attendance records
    IQueryable<Attendances> attendances = db.Attendances
        .Where(a => a.Enrollments_Attendance == Id)
        .Include(e => e.Enrollments.Individuals)
        .OrderByDescending(a => a.AttendanceDate);

    ViewBag.EnrollmentId = Id;

    var sql = attendances.ToString();
    return View(attendances.ToList());
} 

模特:

namespace SlamJammersData.Model
{
    public class Attendances
    {
        public int Id { get; set; }

        [Required]
        [StringLength(15)]
        public string Type { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:MM-dd-yyyy}", ApplyFormatInEditMode = true)]
        public DateTime AttendanceDate { get; set; }

        [StringLength(255)]
        public string Comments { get; set; }

        [Display(Name="Enrollments")]
        public int Enrollments_Attendance { get; set; }

        public virtual Enrollments Enrollments { get; set; }

        [NotMapped]
        public string FullName
        {
            get
            {
                var player = Enrollments.Individuals.FirstName + " " + Enrollments.Individuals.LastName;
                return player;
            }
        }
    }
}

我不确定它是否是IEnumerable,但这是调试器中显示的内容: Screenshot of debugger's Auto log

2 个答案:

答案 0 :(得分:1)

由于您的模型为IEnumerable<SlamJammersData.Model.Attendances>FullName属于Attendances属性,因此您无法在FullName块之外使用foreach属性,例如你正试图这样做。但是,假设FullName的值始终相同,则可以使用ViewBag。更改您的控制器方法如下

public ActionResult Index(int? Id )
{
    //Get the rosters and their attendance records
    IQueryable<Attendances> attendances = db.Attendances
        .Where(a => a.Enrollments_Attendance == Id)
        .Include(e => e.Enrollments.Individuals)
        .OrderByDescending(a => a.AttendanceDate);

    ViewBag.EnrollmentId = Id;
    ViewBag.FullName = ...; // logic to get FullName here

    var sql = attendances.ToString();
    return View(attendances.ToList());
}

并在视图中显示全名,如下所示

@using (Html.BeginForm("Index", "Attendances", FormMethod.Get)) 
{
    <div class="row">
        <div class="col-md-4">
            <p>
                <h3>Attendances for player: @ViewBag.FullName </h3>
            </p>
        </div>
        <div class="col-md-4">
            <p>
                <h4>Attendances Recorded: @Model.Count() </h4>
            </p>
            </div>

        </div>
} 

答案 1 :(得分:0)

我从ASP.NET论坛获得了关于此主题的帮助。所需的帮助程序代码行是:

<h3>Attendances for player: @Html.DisplayName(Model.First().FullName) </h3>

感谢所有看过这个问题的人。

更新:我找到了一行不同的代码,看起来更安全一些。

@Html.DisplayFor(m => m.ElementAt(0).FullName)

更新: 我发现,正如Marko评论的那样,如果记录计数为0,则会抛出异常。因此,如果没有要显示的记录,我会在控制器中使用try-catch来显示更友好的消息。

        public ActionResult Index(int? Id)
    {

        //Get the rosters and their attendance records
                    IQueryable<Attendances> attendances = db.Attendances
                        .Where(a => a.Enrollments_Attendance == Id)
                        .Include(e => e.Enrollments.Individuals)
                        .OrderByDescending(a => a.AttendanceDate);

                    ViewBag.EnrollmentId = Id;
        try
        {
            ViewBag.FullName = attendances.First().FullName;

        }
        catch (InvalidOperationException ex)
        {
            ViewBag.Exception = ex;
           ViewBag.FullName = "No Attendances to display for the selected player";
        }

        return View(attendances.ToList());

    }