Concat名字和姓氏MVC5

时间:2015-09-12 15:40:58

标签: c# entity-framework linq

我对C#和MVC很陌生,我试图将名字和姓氏一起显示为" Full Name"。以下代码试图显示患者的全名(谁是用户)

public class ApplicationUser : IdentityUser
{

    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [Display(Name = "Last Name")]
    public string LastName { get; set; }

     public string FullName
        {
      get
      {
           return FirstName + " " + LastName;
      }

我的预订模式:

public class Booking
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid BookingId { get; set; }

    [ForeignKey("Patient")]
    public Guid PatientId { get; set; }
    public virtual Patient Patient { get; set; }
    }

预订视图模型:

public class BookingViewModel
{
    [Display (Name = "Select Patient")]
    public Guid PatientId { get; set; }
    public IEnumerable<SelectListItem> PatientList { get; set; }
}

控制器:

 public ActionResult Create()
    {
        // Creates a new booking
        BookingViewModel bookingViewModel = new BookingViewModel();
        // Initilises Select List
        ConfigureCreateViewModel(bookingViewModel);

        return View(bookingViewModel);

    }

    // Initilises Select List 
    public void ConfigureCreateViewModel(BookingViewModel bookingViewModel)
    {


        // Displays Patients name 
        bookingViewModel.PatientList = db.Patients.Select(p => new SelectListItem()
        {
            Value = p.PatientId.ToString(),
            Text = p.User.FullName
        });

        }

    // Allows Admin to create booking for patient 
    // POST: Bookings1/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(BookingViewModel bookingViewModel)
    {


        // if model state is not valid
        if (!ModelState.IsValid)
        {
            // Initilises Select lists
            ConfigureCreateViewModel(bookingViewModel);
            return View(bookingViewModel); // returns user to booking page


        }
        else // if model state is Valid
        {


            booking.PatientId = bookingViewModel.PatientId;

            booking.BookingId = Guid.NewGuid();
            // Adds booking to database
            db.Bookings.Add(booking);
            // Saves changes to Database
            db.SaveChanges();
            // Redirects User to Booking Index
            return RedirectToAction("Index");
        }
    }

我的观点:

 <div class="form-group">
        @Html.LabelFor(model => model.PatientId, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.PatientId, Model.PatientList, "-Please select-", new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.PatientId, "", new { @class = "text-danger" })
        </div>
    </div>

但是引发了以下错误:

  

类型&#39; System.NotSupportedException&#39;的例外情况发生在EntityFramework.SqlServer.dll中但未在用户代码中处理

     

其他信息:指定的类型成员&#39; FullName&#39; LINQ to Entities不支持。仅支持初始化程序,实体成员和实体导航属性。

非常感谢任何帮助,谢谢

3 个答案:

答案 0 :(得分:2)

我相信您需要将代码修改为:

bookingViewModel.PatientList = db.Patients.Select(p => new SelectListItem()
        {
            Value = p.PatientId.ToString(),
            Text = p.User.FirstName + " " + p.User.LastName
        });

此类错误的原因是Entity Framework在构建SQL查询时不知道如何映射“FullName”。有关更详细的说明,请参阅以下答案:

The specified type member 'UsersCount' is not supported in LINQ to Entities

答案 1 :(得分:1)

尝试将[NotMapped]属性添加到您计算的属性中以避免此问题。

答案 2 :(得分:0)

       string Employee=a.EmployeeId!=null ? UowObj.EmployeeRepository.GetAllDate().where(x=>x.ID==a.EmployeeId).
Select(c=>c.FirstName+" "+c.MiddleName+" "+c.LastName).FirstOrDefault():"",