C# 没有类型为';IEnumerable<;选择列表项>';那有钥匙';实践';-MVC5
我对MVC非常陌生,刚刚在我的创建页面中添加了一个级联下拉列表,因此当选择某个机构时,眼镜商下拉列表中会填充在该机构工作的眼镜商的名称 型号:C# 没有类型为';IEnumerable<;选择列表项>';那有钥匙';实践';-MVC5,c#,asp.net-mvc,visual-studio,razor,drop-down-menu,C#,Asp.net Mvc,Visual Studio,Razor,Drop Down Menu,我对MVC非常陌生,刚刚在我的创建页面中添加了一个级联下拉列表,因此当选择某个机构时,眼镜商下拉列表中会填充在该机构工作的眼镜商的名称 型号: public class Booking { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid BookingId { get; set; } [ForeignKey("Patient")] public Guid Pa
public class Booking
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid BookingId { get; set; }
[ForeignKey("Patient")]
public Guid PatientId { get; set; }
public virtual Patient Patient { get; set; }
public IEnumerable<SelectListItem> PatientList { get; set; }
[ForeignKey("Practice")]
public Guid PracticeId { get; set; }
public virtual Practice Practice { get; set; }
public IEnumerable<SelectListItem> PracticeList { get; set; }
[ForeignKey("Optician")]
public Guid OpticianId { get; set; }
public virtual Optician Optician { get; set; }
public IEnumerable<SelectListItem> OpticiansList { 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 IEnumerable<SelectListItem> TimeList { get; set; }
public bool isAvail { get; set; }
}
我的看法是:
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script>
$(document).ready(function () {
$("#Optician").prop("disabled", true);
$("#Practice").change(function () {
$.ajax({
url : "@Url.Action("Opticians","Bookings")",
type : "POST",
data : {Id : $(this).val() }
}).done(function (opticianList) {
$("#Optician").empty();
for (var i = 0; i < opticianList.length; i++) {
$("#Optician").append("<option>" + opticianList[i] + "</option>");
}
$("#Optician").prop("disabled", false);
});
});
});
</script>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Booking</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<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>
<div class="form-group">
@Html.LabelFor(model => model.PracticeId, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Practice", ViewData["Practice"] as SelectList,"-Please Select-", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.PracticeId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.Label("Select Optician :", new { @class = "col-md-2 control-label" })
<div class="col-md-10">
<select id="Optician"></select>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TimeId, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.TimeId, Model.TimeList, "-Please select-", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.TimeId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
$(文档).ready(函数(){
$(“#眼镜商”).prop(“残疾”,真);
$(“#实践”)。更改(功能){
$.ajax({
url:“@url.Action”(“眼镜商”、“预订”)”,
类型:“POST”,
数据:{Id:$(this).val()}
}).完成(功能(眼镜商列表){
$(“#眼镜商”).empty();
对于(变量i=0;i
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.PatientId,新的{@class=“controllabel col-md-2”})
@Html.DropDownListFor(model=>model.PatientId,model.PatientList,“-请选择-”,新建{@class=“form control”})
@Html.ValidationMessageFor(model=>model.PatientId,“,new{@class=“text danger”})
@LabelFor(model=>model.PracticeId,新的{@class=“controllabel col-md-2”})
@DropDownList(“Practice”,ViewData[“Practice”]作为SelectList,“-请选择-”,新建{@class=“form control”})
@Html.ValidationMessageFor(model=>model.PracticeId,“,new{@class=“text danger”})
@标签(“选择眼镜商:”,新的{@class=“col-md-2控制标签”})
@LabelFor(model=>model.Date,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Date,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Date,“,new{@class=“text danger”})
@LabelFor(model=>model.TimeId,新的{@class=“controllabel col-md-2”})
@DropDownListFor(model=>model.TimeId,model.TimeList,“-请选择-”,新建{@class=“form control”})
@Html.ValidationMessageFor(model=>model.TimeId,“,new{@class=“text danger”})
}
@ActionLink(“返回列表”、“索引”)
“层叠”下拉列表的工作方式与应有的一样。但是,当我单击按钮创建约会时,会引发以下异常:
例外情况:
System.Web.Mvc.dll中发生“System.InvalidOperationException”类型的异常,但未在用户代码中处理
其他信息:“IEnumerable”类型的ViewData项没有键“Practice”
任何帮助都将不胜感激。
谢谢您的模型已经包含用于实践集合的属性
public IEnumerable<SelectListItem> PracticeList { get; set; }
在GET方法中,您为实践创建一个新的SelectList
,但不是将其指定给模型属性,而是使用
ViewData["Practice"] = practices;
然后在视图中使用
@Html.DropDownList("Practice", ViewData["Practice"] as SelectList, ..)
它甚至不绑定到模型中的属性,也不会回发到任何内容。然后,当您在POST方法中返回视图时(因为您的模式总是无效的),您没有为ViewData[“Practice”]
赋值,因此它为空,因此会出现错误
相反,在您的ConfigureCreateModel()
方法中,填充PracticeList
属性(就像您对PatientList
所做的那样),并删除视图数据的使用,以及在视图中使用
@Html.DropDownListFor(model => model.PracticeId, Model.PracticeList, ...)
因此,当您提交表单时,PracticeId
的值将是所选实践的值,并且您的模型具有很强的约束力
旁注:您需要将脚本更改为$(“#PracticeId”)。更改(函数(){…
,因为在POST方法中,ViewData[“Practice”]
为null
,请阅读我对您的回答,再次感谢您的帮助。我已相应地更新了所有内容,并将practice和optician添加到ConfigCreateModel(),但它不会保存到数据库,并引发异常:“INSERT语句与外键约束冲突\”FK_dbo.Bookings_dbo.Opticians_Opticians id \“您视图中的任何位置都不需要为属性opticiansid
生成表单控件,因此当您尝试保存时,它将始终为null
。
@Html.DropDownList("Practice", ViewData["Practice"] as SelectList, ..)
@Html.DropDownListFor(model => model.PracticeId, Model.PracticeList, ...)