Reputation: 179
I am very new to C# and MVC and I trying to display a First and Last name together as "Full Name". The following code is trying to display A patient's full name (who is a user)
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;
}
My Booking Model :
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; }
}
Booking View Model:
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");
}
}
My View:
<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>
However the following error is being thrown:
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.
Any help would be greatly appreciated, Thanks
Upvotes: 1
Views: 3356
Reputation: 619
string Employee=a.EmployeeId!=null ? UowObj.EmployeeRepository.GetAllDate().where(x=>x.ID==a.EmployeeId).
Select(c=>c.FirstName+" "+c.MiddleName+" "+c.LastName).FirstOrDefault():"",
Upvotes: 0
Reputation: 265
I believe you will need to amend your code to:
bookingViewModel.PatientList = db.Patients.Select(p => new SelectListItem()
{
Value = p.PatientId.ToString(),
Text = p.User.FirstName + " " + p.User.LastName
});
The reason for this type of error is that Entity Framework does not know how to map "FullName" when it is building the SQL query. Please see the following answer for a more detailed description:
The specified type member 'UsersCount' is not supported in LINQ to Entities
Upvotes: 2
Reputation: 6023
Try adding the [NotMapped]
attribute to your calculated property to avoid this problem.
Upvotes: 1