MVC:传入字典的模型项是X类型,但是这个字典需要X类型的模型项

发布于 2025-01-08 18:40:15 字数 1628 浏览 2 评论 0原文

我首先使用 EF 模型并在 MVC 中使用视图模型,我遇到此错误问题:

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[NKI3.Models.Question]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[NKI3.ViewModels.IndexCreateViewModel]'.

这是我的视图模型:

public class IndexCreateViewModel
{
    public string QuestionText { get; set; }
    public string Sname { get; set; }
    public string Cname {get;set;}
}

这是我控制器中的操作:

public ActionResult Index()
{
    var Q = db.Question.Include(a => a.SubjectType).Include(a => a.CoreValue);
    return View(Q.ToList());   
}

这是我的强类型视图:

@model IEnumerable<NKI3.ViewModels.IndexCreateViewModel>

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            QuestionText
        </th>
        <th>
            Sname
        </th>
        <th>
            Cname
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.QuestionText)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Sname)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Cname)
        </td>

    </tr>
}

</table>

我似乎找不到此问题的解决方案,请执行以下操作:我必须在控制器操作索引中返回我的视图模型?任何帮助表示赞赏。

提前致谢!

此致!

I am using EF model first and using viewmodels in MVC, I have problems with this error:

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[NKI3.Models.Question]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[NKI3.ViewModels.IndexCreateViewModel]'.

This is my viewmodel:

public class IndexCreateViewModel
{
    public string QuestionText { get; set; }
    public string Sname { get; set; }
    public string Cname {get;set;}
}

This is my Action in my controller:

public ActionResult Index()
{
    var Q = db.Question.Include(a => a.SubjectType).Include(a => a.CoreValue);
    return View(Q.ToList());   
}

Here is my strongly typed view:

@model IEnumerable<NKI3.ViewModels.IndexCreateViewModel>

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            QuestionText
        </th>
        <th>
            Sname
        </th>
        <th>
            Cname
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.QuestionText)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Sname)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Cname)
        </td>

    </tr>
}

</table>

I dont seem to find the solution for this, do I have to return my viewmodel inside the controller action index? Any help is appreciated.

Thanks in advance!

Best Regards!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆悲凉 2025-01-15 18:40:15

请参阅:下面的注释
返回 Enumerable 有帮助吗?

 return View(Q.AsEnumerable()); 

编辑:是的,我知道我的第一个镜头是错误的!...没有注意到你的观点想要一个

@model IEnumerable<NKI3.ViewModels.IndexCreateViewModel>

我同意其他人关于转换为匹配类型所需的返回值...
或调整视图中的预期类型...
也许您可以利用像 automapper (http://automapper.org/) 这样的映射框架来帮助您解决域对象和视图模型之间的映射代码。

这取决于你 - 无需进一步投票......


See: note down below
Would it help to return Enumerable?

 return View(Q.AsEnumerable()); 

EDIT: Yeah, I know my first shot was wrong!... did not noticed that your view wants a

@model IEnumerable<NKI3.ViewModels.IndexCreateViewModel>

I agree with the others about converting to the required return value to the matching type...
or adjust the expecting type in the view...
maybe you can take advantage of mapping framworks like automapper (http://automapper.org/) to help you to solve to mapping code between domain objects and viewmodels.

it's up to you - No need for further down voting...

扛刀软妹 2025-01-15 18:40:15

是的 - 该操作应该返回与视图文件第一行声明的相同类型的模型。

将视图文件中的第一行更改为:

@model IndexCreateViewModel

并将操作代码 (Index()) 更改为将返回 IndexCreateViewModel 类型的对象的代码。

Yes - the action should return a model of the same type declared on the first line of your view file.

Change your first line in the view file to:

@model IndexCreateViewModel

and change your action code (Index()) to a code that will return an object of type IndexCreateViewModel.

你的心境我的脸 2025-01-15 18:40:15

返回的类型

db.Question.Include(a => a.SubjectType).Include(a => a.CoreValue);

看起来是 IQueryable;

您需要将该结构(您的数据模型)转换为 IEnumerable

如果您的应用中还没有帮助程序, 如果要在它们之间进行转换,那么您需要编写一个。

IList<IndexCreateViewModel> viewmodel = new List<IndexCreateViewModel>();

foreach(NKI3.Models.Question item in Q)
{
   IndexCreateViewModel viewItem = new IndexCreateViewModel();
   // I don't know the properties of Question.
   viewItem.QuestionText = item.????;
   viewItem.CName = item.???;
   viewItem.SName =-item.???;
   viewModel.Add(viewItem);
}
return View(viewModel);

The type returned by

db.Question.Include(a => a.SubjectType).Include(a => a.CoreValue);

looks to be IQueryable<NKI3.Models.Question>

You need to convert that structure (your data model) to an IEnumerable<IndexCreateViewModel>

If there isn't already a helper in your app to transform between them then you'll need to write one.

IList<IndexCreateViewModel> viewmodel = new List<IndexCreateViewModel>();

foreach(NKI3.Models.Question item in Q)
{
   IndexCreateViewModel viewItem = new IndexCreateViewModel();
   // I don't know the properties of Question.
   viewItem.QuestionText = item.????;
   viewItem.CName = item.???;
   viewItem.SName =-item.???;
   viewModel.Add(viewItem);
}
return View(viewModel);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文