Je suis très nouveau à C# et MVC et j'essaye d'afficher un prénom et un nom ensemble comme "nom complet". Le code suivant tente d'afficher un nom du patient complet (qui est un utilisateur)Concat Prénom et Nom MVC5
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;
}
Ma Réservation Modèle:
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; }
}
Réservation Voir Modèle:
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");
}
}
Ma vue:
<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>
Cependant, l'erreur suivante est générée:
Une exception de type 'System.NotSupportedException' a eu lieu en EntityFramework.SqlServer.dll mais n'a pas été traitée dans le code utilisateur
Informations complémentaires : Le membre de type spécifié 'FullName' n'est pas pris en charge dans LINQ to Entities. Seuls les initialiseurs, les membres d'entité et les propriétés de navigation d'entité sont pris en charge.
Toute aide serait grandement apprécié, Merci
Merci beaucoup – coto2