5: Show Full Names

I would like to show a FullName from a computed property at the top of a view. I can see the FullName property when I look at the Autos log. But the lambda expression used in the Html helper only shows the property name, not the data. Is there any way to display the data that the log seems to show to be available?

UPDATE:

The Complete View:

@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>

The Controller:

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());
} 

The Model:

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;
            }
        }
    }
}

I am not sure if it is IEnumerable or not, but here is what is showing in the debugger: Screenshot of debugger's Auto log


Since your model is IEnumerable<SlamJammersData.Model.Attendances> and FullName is a property of Attendances , you can't use FullName property outside foreach block like what you're trying to do. However assuming that the value of FullName is always the same, you can use ViewBag . Change your controller method as below

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());
}

and display the full name in your view as follows

@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>
} 

I got an assist from the ASP.NET forum on this topic. The helper code line needed is:

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

Thanks to everyone who took a look at this question.

UPDATE: I found a different line of code that works and seems a little safer.

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

UPDATE: I found that as Marko commented, there was an exception thrown if the record count is 0. So I did a work around using a try-catch in the controller to display a friendlier message if there are no records to display.

        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());

    } 
链接地址: http://www.djcxy.com/p/19088.html

上一篇: 由于表达式的原因DropDownListFor在局部视图中失败

下一篇: 5:显示完整名称