没有类型为'IEnumerable< SelectListItem>'的ViewData项 [英] There is no ViewData item of type 'IEnumerable<SelectListItem>'

查看:85
本文介绍了没有类型为'IEnumerable< SelectListItem>'的ViewData项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我花了将近10个小时,谷歌阅读接近100页但仍然无法解决。



错误消息 - 附加信息:没有类型为'IEnumerable< SelectListItem>'的ViewData项目,其键有'ChartNo'



我的课程。



头等舱




公共课eChart

{

[Key]

[显示(姓名=图号)]

public int ChartNo {get;组; }



[必需,StringLength(25),显示(名称=名字)]

公共字符串PatientFirstName {get;组; }



[必需,StringLength(25),显示(姓名=姓氏)]

公共字符串PatientLastName {get;组; }



[必需(ErrorMessage =需要移动)]

[RegularExpression(@^ \(?([0 -9] {3})\)?[ - 。]?([0-9] {3})[ - 。]?([0-9] {4})$,ErrorMessage =输入的手机格式无效。)]

[数据类型(DataType.PhoneNumber)]

[显示(姓名=电话号码)]

公共字符串PatientPhoneNo {get;组; }



公共虚拟ICollection< treatment>治疗{得到;组; }

}



第二课



公共课程待遇

{

[Key]

public int TreatmentNo {get;组; }



[必填,显示(名称=治疗状态)]

公共字符串TreatmentStatus {get;组; }



[显示(名称=初始访问日期)]

public DateTime InitialVisit {get;组; }



[显示(姓名=上次访问日期)]

public DateTime LastVisit {get;组; }



[显示(姓名=下次访问日期)]

public DateTime NextVisit {get;组; }





public int ChartNo {get;组; } $ / $


公共虚拟eChart eChart {get;组; }



}

观看次数



索引(有标签,其中一个标签使用局部视图)



 @ {
ViewBag.Title = eChartHome;
布局= 〜/ Views / Shared / _Layout.cshtml;
}

< div = container >
<!-------->
< div id = content >
< ul id = tabs = nav nav-tabs data-tabs = 标签 >
< li class = active>< a href = #search data-toggle = tab>搜索< / a > < / li >
< li>< a href = #createupdatechart数据-toggle = tab>创建/更新 - 图表< / a > < ; / li >
< li>< a href = #treatments data-toggle = tab> Treatments < / a > < / li >
< / ul <温泉n class =code-keyword>>
< div id = my -tab-content class = 标签内容 >

@ *创建/更新标签 - 从这里开始* @
< div class = tab-pane id = createupdatechart >
@ *< h3 class = text-成功>创建图表!< / h3 > * @

< div class = container >
< div class = row >
< div class = col-md-9 >
< div class = panel panel-info >
< div class = 面板-heading >
< h5>创建图表。 < / h5 >
< / < span class =code-leadattribute> div >
< div class = panel-body >
@ Html.Partial( _ createChartPartial
< / div >
< span class =code-keyword>< / div >
< / div >

< / div > ;
< / div >
< / div >
@ *创建/更新标签 - 在此结束* @

< div class = tab-pane id = 处理 >
@ *< h3 class = text-success>创建图表!< / h3 > * @

< div class = 容器 >
< div class = <跨度class =code-string> row >
< div class = col-md-9 >
< div class = 面板 - 信息 >
< div class = panel-heading >
< h5>创建处理。< / h5 >
< / div >
< div class = < span class =code-string> panel-body >
@ Html.Partial( _ createTreatmentPartial)@ *这里我调用部分视图.. 抛出错误。如果我只是输入 正文,它就可以了。
< / div >
< / div >
< / div >

< / div >
< / div >
< / div >
@ *创建/更新标签 - 此处结束* @


< / div >
<脚本 type = text / javascript >
jQuery(document).ready(function($){
$( ' #tabs')。tab();
});
< / script >
< / div > <! - 容器 - >







部分视图



 @ model ModalBootstrap.Models.Treatment 

@using(Html.BeginForm( ))
{
@ Html.AntiForgeryToken()

< div class = form-horizo​​ntal >
< h4> ;处理< / h4 >

@ Html.ValidationSummary( true

< div class = form-group >
@ Html.LabelFor(model = > model.TreatmentStatus, new {@class = control-label col-md-2})
< div class = col-md-10 >
@Html。 EditorFor(model = > model.TreatmentStatus)
@ Html.ValidationMessageFor(model = > model.TreatmentStatus)
< / div >
< / div >
< div = form-group >
@ Html.LabelFor(model = > model.ChartNo, ChartNo new {@class = control-label col-md-2 })
< div class = col-md-10 >
@ * @ Html.DropDownList(model = > model.ChartNo,ViewData [ ChartNo] as SelectList)* @ *

@ Html.DropDownList( ChartNo)@ *我尝试了所有这些不同的方式......没有用!! * @
@ * @ Html.DropDownList( ChartNo,ViewBag.Roles as SelectList)
@Html。 DropDownList( ChartNo null 选择图表 null )* @
@ Html.ValidationMessageFor(model = > model.ChartNo)
< / div >
< / div >

< div = form-group >
< div class = col-md-offset-2 col-md-10 >
< input type = submit value = 创建 class = btn btn-default />
< / div >
< / div >
< / div >
}

< div>
@ Html.ActionLink( 返回列表 索引
< / div >





控制器

 public ActionResult Create()
{
// List < SelectListItem > item = new SelectList(db.eCharts,ChartNo );

// ViewBag.ChartNo = item;
// ViewData [ChartNo] =新的SelectList(db.eCharts,ChartNo,PatientFirstName);

ViewData [ChartNo] =新的SelectList(db.eCharts,ChartNo,ChartNo);


return View();
}

解决方案

,ErrorMessage =输入的手机格式无效。)]

[DataType(DataType.PhoneNumber)]

[显示(姓名=电话号码)]

公共字符串PatientPhoneNo {get; set;}



公共虚拟ICollection<治疗>治疗{get; set;}

}



第二课



公共课治疗

{

[Key]

public int TreatmentNo {get; set;}



[必填,显示(姓名=治疗状态)]

公共字符串TreatmentStatus {get; set;}



[显示(名称=初始访问日期 )]

public DateTime InitialVisit {get; set;}



[显示(姓名=上次访问日期)]

public DateTime LastVisit {get; set;}



[显示(名称=下次访问日期)]

public DateTime NextVis它{得到;组; }





public int ChartNo {get;组; } $ / $


公共虚拟eChart eChart {get;组; }



}

观看次数



索引(有标签,其中一个标签使用局部视图)



 @ {
ViewBag.Title = eChartHome;
布局= 〜/ Views / Shared / _Layout.cshtml;
}

< div = container >
<!-------->
< div id = content >
< ul id = tabs = nav nav-tabs data-tabs = 标签 >
< li class = active>< a href = #search data-toggle = tab>搜索< / a > < / li >
< li>< a href = #createupdatechart数据-toggle = tab>创建/更新 - 图表< / a > < ; / li >
< li>< a href = #treatments data-toggle = tab> Treatments < / a > < / li >
< / ul <温泉n class =code-keyword>>
< div id = my -tab-content class = 标签内容 >

@ *创建/更新标签 - 从这里开始* @
< div class = tab-pane id = createupdatechart >
@ *< h3 class = text-成功>创建图表!< / h3 > * @

< div class = container >
< div class = row >
< div class = col-md-9 >
< div class = panel panel-info >
< div class = 面板-heading >
< h5>创建图表。 < / h5 >
< / < span class =code-leadattribute> div >
< div class = panel-body >
@ Html.Partial( _ createChartPartial
< / div >
< span class =code-keyword>< / div >
< / div >

< / div > ;
< / div >
< / div >
@ *创建/更新标签 - 在此结束* @

< div class = tab-pane id = 处理 >
@ *< h3 class = text-success>创建图表!< / h3 > * @

< div class = 容器 >
< div class = <跨度class =code-string> row >
< div class = col-md-9 >
< div class = 面板 - 信息 >
< div class = panel-heading >
< h5>创建处理。< / h5 >
< / div >
< div class = < span class =code-string> panel-body >
@ Html.Partial( _ createTreatmentPartial)@ *这里我调用部分视图.. 抛出错误。如果我只是输入 正文,它就可以了。
< / div >
< / div >
< / div >

< / div >
< / div >
< / div >
@ *创建/更新标签 - 此处结束* @


< / div >
<脚本 type = text / javascript >
jQuery(document).ready(function(


< blockquote>){


' #tabs')。标签();
});
< / script >
< / div > <! - 容器 - >







部分视图



 @ model ModalBootstrap.Models.Treatment 

@using(Html.BeginForm( ))
{
@ Html.AntiForgeryToken()

< div class = form-horizo​​ntal >
< h4> ;处理< / h4 >

@ Html.ValidationSummary( true

< div class = form-group >
@ Html.LabelFor(model = > model.TreatmentStatus, new {@class = control-label col-md-2})
< div class = col-md-10 >
@Html。 EditorFor(model = > model.TreatmentStatus)
@ Html.ValidationMessageFor(model = > model.TreatmentStatus)
< / div >
< / div >
< div = form-group >
@ Html.LabelFor(model = > model.ChartNo, ChartNo new {@class = control-label col-md-2 })
< div class = col-md-10 >
@ * @ Html.DropDownList(model = > model.ChartNo,ViewData [ ChartNo] as SelectList)* @ *

@ Html.DropDownList( ChartNo)@ *我尝试了所有这些不同的方式......没有用!! * @
@ * @ Html.DropDownList(\"ChartNo\", ViewBag.Roles as SelectList)
@Html.DropDownList(\"ChartNo\", null, \"Select Chart\", null)*@
@Html.ValidationMessageFor(model => model.ChartNo)
</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>





Controller

public ActionResult Create() 
{
// List<SelectListItem> item = new SelectList(db.eCharts, \"ChartNo\");

// ViewBag.ChartNo = item;
//ViewData[\"ChartNo\"] = new SelectList(db.eCharts, \"ChartNo\", \"PatientFirstName\");

ViewData[\"ChartNo\"] = new SelectList(db.eCharts, \"ChartNo\", \"ChartNo\");


return View();
}


Hi,

I have spent close to 10 hours on this, read close to 100 pages in google and still not able to resolve.

error message - Additional information: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'ChartNo'

My Classes.

1st class


public class eChart
{
[Key]
[Display(Name="Chart No.")]
public int ChartNo { get; set; }

[Required, StringLength(25), Display(Name="First Name")]
public string PatientFirstName { get; set; }

[Required, StringLength(25), Display(Name = "Last Name")]
public string PatientLastName { get; set; }

[Required(ErrorMessage = "Mobile is required")]
[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Entered phone format is not valid.")]
[DataType(DataType.PhoneNumber)]
[Display(Name = "Phone number")]
public string PatientPhoneNo { get; set; }

public virtual ICollection<treatment> Treatments { get; set; }
}

2nd class


public class Treatment
{
[Key]
public int TreatmentNo { get; set; }

[Required, Display(Name="Treatment Status")]
public string TreatmentStatus { get; set; }

[Display(Name = "Initial Visit Date")]
public DateTime InitialVisit { get; set; }

[Display(Name = "Last Visit Date")]
public DateTime LastVisit { get; set; }

[Display(Name = "Next Visit Date")]
public DateTime NextVisit { get; set; }


public int ChartNo { get; set; }

public virtual eChart eChart { get; set; }

}
Views

Index (has tabs, and one of the tab uses partial view)

@{
ViewBag.Title = "eChartHome";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="container">
<!-------->
<div id="content">
<ul id="tabs" class="nav nav-tabs" data-tabs="tabs">
<li class="active"><a href="#search" data-toggle="tab">Search</a></li>
<li><a href="#createupdatechart" data-toggle="tab">Create / Update - Chart</a></li>
<li><a href="#treatments" data-toggle="tab">Treatments</a></li>
</ul>
<div id="my-tab-content" class="tab-content">

@* Create / Update Tab - Start here*@
<div class="tab-pane" id="createupdatechart">
@* <h3 class="text-success">Create a new chart!</h3>*@

<div class="container">
<div class="row">
<div class="col-md-9">
<div class="panel panel-info">
<div class="panel-heading">
<h5>Create a new Chart.</h5>
</div>
<div class="panel-body">
@Html.Partial("_createChartPartial")
</div>
</div>
</div>

</div>
</div>
</div>
@* Create / Update Tab - Ends here*@

<div class="tab-pane" id="treatments">
@* <h3 class="text-success">Create a new chart!</h3>*@

<div class="container">
<div class="row">
<div class="col-md-9">
<div class="panel panel-info">
<div class="panel-heading">
<h5>Create a new Treatment.</h5>
</div>
<div class="panel-body">
@Html.Partial("_createTreatmentPartial")   @* here i am calling the partial view.. this is the one throws error. If I simply type "body text", it works. 
</div>
</div>
</div>

</div>
</div>
</div>
@* Create / Update Tab - Ends here*@


</div>
<script type="text/javascript">
jQuery(document).ready(function ($) {
$('#tabs').tab();
});
</script>
</div> <!-- container -->




Partial view

@model ModalBootstrap.Models.Treatment

@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>Treatment</h4>

@Html.ValidationSummary(true)

<div class="form-group">
@Html.LabelFor(model => model.TreatmentStatus, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TreatmentStatus)
@Html.ValidationMessageFor(model => model.TreatmentStatus)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ChartNo, "ChartNo", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@* @Html.DropDownList(model => model.ChartNo, ViewData["ChartNo"] as SelectList)*@*

@Html.DropDownList("ChartNo") @* I have tried all these different ways...nothing works!!*@
@* @Html.DropDownList("ChartNo", ViewBag.Roles as SelectList)
@Html.DropDownList("ChartNo", null, "Select Chart", null)*@
@Html.ValidationMessageFor(model => model.ChartNo)
</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>



Controller

public ActionResult Create()
       {
           // List<SelectListItem> item = new SelectList(db.eCharts, "ChartNo");

         //   ViewBag.ChartNo = item;
           //ViewData["ChartNo"] = new SelectList(db.eCharts, "ChartNo", "PatientFirstName");

         ViewData["ChartNo"] = new SelectList(db.eCharts, "ChartNo", "ChartNo");

          
           return View();
       }

解决方案

", ErrorMessage = "Entered phone format is not valid.")]
[DataType(DataType.PhoneNumber)]
[Display(Name = "Phone number")]
public string PatientPhoneNo { get; set; }

public virtual ICollection<treatment> Treatments { get; set; }
}

2nd class


public class Treatment
{
[Key]
public int TreatmentNo { get; set; }

[Required, Display(Name="Treatment Status")]
public string TreatmentStatus { get; set; }

[Display(Name = "Initial Visit Date")]
public DateTime InitialVisit { get; set; }

[Display(Name = "Last Visit Date")]
public DateTime LastVisit { get; set; }

[Display(Name = "Next Visit Date")]
public DateTime NextVisit { get; set; }


public int ChartNo { get; set; }

public virtual eChart eChart { get; set; }

}
Views

Index (has tabs, and one of the tab uses partial view)

@{
ViewBag.Title = "eChartHome";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="container">
<!-------->
<div id="content">
<ul id="tabs" class="nav nav-tabs" data-tabs="tabs">
<li class="active"><a href="#search" data-toggle="tab">Search</a></li>
<li><a href="#createupdatechart" data-toggle="tab">Create / Update - Chart</a></li>
<li><a href="#treatments" data-toggle="tab">Treatments</a></li>
</ul>
<div id="my-tab-content" class="tab-content">

@* Create / Update Tab - Start here*@
<div class="tab-pane" id="createupdatechart">
@* <h3 class="text-success">Create a new chart!</h3>*@

<div class="container">
<div class="row">
<div class="col-md-9">
<div class="panel panel-info">
<div class="panel-heading">
<h5>Create a new Chart.</h5>
</div>
<div class="panel-body">
@Html.Partial("_createChartPartial")
</div>
</div>
</div>

</div>
</div>
</div>
@* Create / Update Tab - Ends here*@

<div class="tab-pane" id="treatments">
@* <h3 class="text-success">Create a new chart!</h3>*@

<div class="container">
<div class="row">
<div class="col-md-9">
<div class="panel panel-info">
<div class="panel-heading">
<h5>Create a new Treatment.</h5>
</div>
<div class="panel-body">
@Html.Partial("_createTreatmentPartial")   @* here i am calling the partial view.. this is the one throws error. If I simply type "body text", it works. 
</div>
</div>
</div>

</div>
</div>
</div>
@* Create / Update Tab - Ends here*@


</div>
<script type="text/javascript">
jQuery(document).ready(function (


) {


('#tabs').tab(); }); </script> </div> <!-- container -->




Partial view

@model ModalBootstrap.Models.Treatment

@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>Treatment</h4>

@Html.ValidationSummary(true)

<div class="form-group">
@Html.LabelFor(model => model.TreatmentStatus, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TreatmentStatus)
@Html.ValidationMessageFor(model => model.TreatmentStatus)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ChartNo, "ChartNo", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@* @Html.DropDownList(model => model.ChartNo, ViewData["ChartNo"] as SelectList)*@*

@Html.DropDownList("ChartNo") @* I have tried all these different ways...nothing works!!*@
@* @Html.DropDownList("ChartNo", ViewBag.Roles as SelectList)
@Html.DropDownList("ChartNo", null, "Select Chart", null)*@
@Html.ValidationMessageFor(model => model.ChartNo)
</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>



Controller

public ActionResult Create()
       {
           // List<SelectListItem> item = new SelectList(db.eCharts, "ChartNo");

         //   ViewBag.ChartNo = item;
           //ViewData["ChartNo"] = new SelectList(db.eCharts, "ChartNo", "PatientFirstName");

         ViewData["ChartNo"] = new SelectList(db.eCharts, "ChartNo", "ChartNo");

          
           return View();
       }


这篇关于没有类型为'IEnumerable&lt; SelectListItem&gt;'的ViewData项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆