gpt4 book ai didi

c# - Concat 名字和姓氏 MVC5

转载 作者:太空宇宙 更新时间:2023-11-03 23:30:15 24 4
gpt4 key购买 nike

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

预订 View 模型:

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

Controller :

 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>

但是会抛出以下错误:

An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

Additional information: The specified type member 'FullName' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

非常感谢任何帮助,谢谢

最佳答案

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

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

关于c# - Concat 名字和姓氏 MVC5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32540639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com