Entity framework 无法将switch语句放入mvc 4中的foreach循环中

Entity framework 无法将switch语句放入mvc 4中的foreach循环中,entity-framework,asp.net-mvc-4,Entity Framework,Asp.net Mvc 4,这是我的控制器,我有一个字符串数组,通过ViewData我向view发送数据 [HttpGet] public PartialViewResult GetMeataDataDetails(int id) { tr_upld_content upld1 = new tr_upld_content(); string[] contentlabel = db.tr_doc_content.Where(x => x.doc_typeid == id).Select(x=>x.

这是我的控制器,我有一个字符串数组,通过
ViewData
我向view发送数据

[HttpGet]
public PartialViewResult GetMeataDataDetails(int id)
{
    tr_upld_content upld1 = new tr_upld_content();
    string[] contentlabel = db.tr_doc_content.Where(x => x.doc_typeid == id).Select(x=>x.doc_contenttypelabel).ToArray();
    ViewData["passedarray"] = contentlabel;
    string[] ctrltype = db.tr_doc_content.Where(x => x.doc_typeid == id).Select(x => x.doc_ctrltype).ToArray();
    ViewData["ctrltype"] = ctrltype;
    string[] fieldtype = db.tr_doc_content.Where(x => x.doc_typeid == id).Select(x => x.doc_fieldtype).ToArray();
    return PartialView(upld1);
}
这是我的视图代码

@model C3KYCSystem.tr_upld_content
<h2>GetMeataDataDetails</h2>
@foreach (string lbl in ViewData["passedArray"] as string[])
{
    <br />
    <tr>@lbl</tr>
}
@foreach (string crl in ViewData["ctrltype"] as string[])
{
    switch(ViewData["ctrltype"].ToString())
    {
        case "textbox":
            <td>@Html.TextBoxFor(m => m.upld_contentvalue)</td>
            break;
    }     
}
@model C3KYCSystem.tr\u upld\u内容
GetMeatadatadials
@foreach(ViewData[“passedArray”]中的字符串lbl作为字符串[])
{

@lbl } @foreach(ViewData[“ctrltype”]中的字符串crl作为字符串[]) { 开关(ViewData[“ctrltype”].ToString()) { 案例“文本框”: @Html.TextBoxFor(m=>m.upld\u contentvalue) 打破 } }
Viewdata[“ctrltype”]
包含文本框、多行文本框等值。如果是文本框,我希望获取编辑器模板等。我想遍历每个字符串数组,检查开关内部是否匹配,然后相应地进行渲染。此时开关未执行。

您需要更改

switch(ViewData["ctrltype"].ToString())

要访问循环中的每个项目,您需要更改

switch(ViewData["ctrltype"].ToString())


要访问循环中的每个项目

可以有任意多个循环。但是FGS,使用视图模型并将其填充到控制器中,而不是使用
ViewData
,这样您就可以强绑定到您的模型。我的控制器中有一个字符串数组。对于每个字符串数组,我希望执行不同的操作。如果我按下开关,我正在努力,否则它工作正常。您的问题是关于多个
foreach
循环的问题。你现在是说问题出在
switch
语句上吗?是的,如果我从second for循环中删除switch,多个for循环工作正常。我现在发现问题出在switch语句上,然后编辑你的问题,包括标题和解释,并显示相关代码,解释你期望的内容和实际发生的事情。你可以有任意多个循环。但是FGS,使用视图模型并将其填充到控制器中,而不是使用
ViewData
,这样您就可以强绑定到您的模型。我的控制器中有一个字符串数组。对于每个字符串数组,我希望执行不同的操作。如果我按下开关,我正在努力,否则它工作正常。您的问题是关于多个
foreach
循环的问题。你现在是说问题出在
switch
语句上吗?是的,如果我从second for循环中删除switch,多个for循环工作正常。我现在发现问题出在switch语句上,然后编辑您的问题,包括标题和解释,并显示相关代码,解释您期望的内容和实际发生的情况