C# MVC操作异常无效#
我对c#和MVC很陌生。我正在尝试创建一个web应用程序来预订一家眼镜店。我已经使用代码优先实体框架创建了数据库。在创建预订页面上,我有一个下拉列表,可以在其中选择眼镜商。当我使用CRUD创建创建页面时,下拉列表显示了用户ID,但是我试图将其更改为应用程序用户(眼镜商)FirstName和LastName的concat(全名) 预订模式:C# MVC操作异常无效#,c#,asp.net-mvc,database,visual-studio-2013,foreign-keys,C#,Asp.net Mvc,Database,Visual Studio 2013,Foreign Keys,我对c#和MVC很陌生。我正在尝试创建一个web应用程序来预订一家眼镜店。我已经使用代码优先实体框架创建了数据库。在创建预订页面上,我有一个下拉列表,可以在其中选择眼镜商。当我使用CRUD创建创建页面时,下拉列表显示了用户ID,但是我试图将其更改为应用程序用户(眼镜商)FirstName和LastName的concat(全名) 预订模式: public class Booking { [DatabaseGenerated(DatabaseGeneratedOption.Identity)
public class Booking
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid BookingId { get; set; }
[ForeignKey("Patient")]
public Guid PatientId { get; set; }
public virtual Patient Patient { get; set; }
[ForeignKey("Practice")]
public Guid PracticeId { get; set; }
public virtual Practice Practice { get; set; }
[ForeignKey("Optician")]
public Guid OpticianId { get; set; }
public virtual Optician Optician { get; set; }
[Display(Name = "Date")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime Date { get; set; }
[ForeignKey("Time")]
public Guid? TimeId { get; set; }
public virtual Time Time { get; set; }
public bool isAvail { get; set; }
}
身份模型:
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 this.FirstName + " " + this.LastName;
}
}
眼镜商:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid OpticianId { get; set; }
[ForeignKey("User")]
public string UserId { get; set; }
public virtual ApplicationUser User { get; set; }
[ForeignKey("Practice")]
public Guid PracticeId { get; set; }
public virtual Practice Practice { get; set; }
public virtual ICollection<Booking> Bookings { get; set; }
public virtual ICollection<Appointment> Appointments { get; set; }
}
创建视图:
<div class="form-group">
@Html.LabelFor(model => model.Optician.User.FullName, "FullName", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("FullName", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Optician.User.FullName, "", new { @class = "text-danger" })
</div>
</div>
@LabelFor(model=>model.Optician.User.FullName,“FullName”,htmlAttributes:new{@class=“control label col-md-2”})
@DropDownList(“FullName”,null,htmlAttributes:new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Optician.User.FullName,“,新的{@class=“text danger”})
我得到以下例外情况:
System.Web.Mvc.dll中发生“System.InvalidOperationException”类型的异常,但未在用户代码中处理
其他信息:“IEnumerable”类型的ViewData项没有键“FullName”
任何帮助都将不胜感激您出现此错误的原因是因为属性
FullName
是字符串,而不是IEnumerable
,并且在您使用的视图中
@Html.DropDownList("FullName", null, ...)
如果提供null
作为第二个参数,则第一个参数必须是IEnumerable
,其中T
是值类型或SelectListItem
在任何情况下,FullName
都是一个只读属性(没有setter),因此它无论如何都不会绑定到您的属性,更不用说您通过糟糕地使用[bind(include=“…”)]
属性将属性从绑定中排除(如果您发现自己正在使用它,则改用视图模型)
由于您的SelectList
构造函数没有意义,因此不清楚您实际想要做什么<代码>新选择列表(db.Opticans、“眼镜商ID”、“用户ID”)表示生成的
元素的值属性等于OpticianId
属性的值,文本等于UserId
属性的值。但是你甚至不在视图中使用它
首先创建一个视图模型,表示要在视图中显示的内容
public class BookingVM
{
[Display(Name = "Optician")]
[Required(ErrorMessage = "Please select an optician")]
public Guid OpticianId { get; set; } // recommend you use int , not Guid
public IEnumerable<SelectListItem> OpticianList { get; set; }
.... // other properties to edit in the view
}
@model BookingVM
....
<div class="form-group">
@Html.LabelFor(m => m.OpticianId, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.OpticianId, Model.OpticianList, "-Please select-", new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.OpticianId, new { @class = "text-danger" })
</div>
</div>
在我看来
public class BookingVM
{
[Display(Name = "Optician")]
[Required(ErrorMessage = "Please select an optician")]
public Guid OpticianId { get; set; } // recommend you use int , not Guid
public IEnumerable<SelectListItem> OpticianList { get; set; }
.... // other properties to edit in the view
}
@model BookingVM
....
<div class="form-group">
@Html.LabelFor(m => m.OpticianId, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.OpticianId, Model.OpticianList, "-Please select-", new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.OpticianId, new { @class = "text-danger" })
</div>
</div>
旁注:视图模型可以包含一个
DateTime MinDate
属性(作为隐藏输入包含在视图中),然后对Date
属性应用一个简单的[GreaterThan]
或类似的属性,这样您就可以立即进行客户端和服务器端验证。限制可选择日期的jquery datepicker插件也将改善用户体验。您的视图与哪种类型的模型关联?@brroshan该视图与预订模型关联
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(BookingVM model)
{
if (!ModelState.IsValid)
{
ConfigureCreateModel(model);
return View(model);
}
// map you view model properties to a new instance of your data model
// save the data model and redirect.
}