【问题标题】:Concat First and Last Name MVC5连接名字和姓氏 MVC5
【发布时间】:2018-06-08 15:55:17
【问题描述】:

我对 C# 和 MVC 非常陌生,我试图将名字和姓氏一起显示为“全名”。下面的代码试图显示一个病人的全名(谁是用户)

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>

但是会抛出以下错误:

EntityFramework.SqlServer.dll 中出现“System.NotSupportedException”类型的异常,但未在用户代码中处理

附加信息:LINQ to Entities 不支持指定的类型成员“FullName”。仅支持初始化程序、实体成员和实体导航属性。

任何帮助将不胜感激,谢谢

【问题讨论】:

    标签: c# entity-framework linq


    【解决方案1】:

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

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

    此类错误的原因是实体框架在构建 SQL 查询时不知道如何映射“FullName”。请参阅以下答案以获得更详细的说明:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 恐怕还在抛出异常Additional information: The specified type member 'FullName' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
      【解决方案3】:
             string Employee=a.EmployeeId!=null ? UowObj.EmployeeRepository.GetAllDate().where(x=>x.ID==a.EmployeeId).
      Select(c=>c.FirstName+" "+c.MiddleName+" "+c.LastName).FirstOrDefault():"",
      

      【讨论】:

      • 特别是对于刚接触 C# 的人,您应该提供更多关于为什么如何您的代码解决问题的信息。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-22
      • 2013-02-05
      • 1970-01-01
      • 1970-01-01
      • 2016-07-12
      • 1970-01-01
      相关资源
      最近更新 更多