Html.Action 无效

Html.Action not work

我想将问题 link 列表的部分视图添加到我的编辑视图中。但是,当我 select groupby 值时,它会显示以下错误。请指导我。

The model item passed into the dictionary is of type 'System.Collections.Generic.List1[<>f__AnonymousType42[SurveyTool.Models.SURV_Question_Ext_Model,SurveyTool.Models.SURV_Question_Model]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[SurveyTool.Models.SURV_Question_Ext_Model]'.

编辑视图:

@model IFXSurveyTool.Models.SURV_Main_Model

    <div class="question">

            <h2>Link</h2>

            @Html.Action("QuestionLink", "SURV_Main", new { Survey_ID = Model.Survey_ID })

        </div>

控制器:

 public ActionResult QuestionLink(int Survey_ID)
        {
            var query = from r in db.SURV_Question_Ext_Model
                        join s in db.SURV_Question_Model on r.Qext_Question_ID equals s.Question_ID
                        where s.Question_Survey_ID == Survey_ID
                        group new { r, s } by r.Qext_Language into grp
                        select grp.FirstOrDefault();

            return PartialView(query.ToList());
        }

问题链接视图:

@model IEnumerable<SurveyTool.Models.SURV_Question_Ext_Model>

<br />
<table class="strip">

    @foreach (var item in Model)
    {
        <tr>
            <td width="5%"></td>
            <td>
                @Html.ActionLink("QuestionLink", "Edit", "SURV_Answer", new { Language = item.Qext_Language }, null)
            </td>


        </tr>
    }

</table>

请更改您的 PartialView 代码中的以下行:

@model IEnumerable<SurveyTool.Models.SURV_Question_Ext_Model>

至:

@model List<SurveyTool.Models.SURV_Question_Ext_Model>

或者在控制器中返回类型。类型需要匹配。

来自评论: 还有一组返回的 LINQ 查询,所以它是一个异常类型,而不是您期望的类型。

你的错误不大。试试这个:

@model IEnumerable<SurveyTool.Models.SURV_Question_Ext_Model>

<br />
<table class="strip">

    @foreach (var item in Model)
    {
        <tr>
            <td width="5%"></td>
            <td>
                @Html.ActionLink("Edit", "QuestionLink", "SURV_Answer", new { Language = item.Qext_Language }, null)
            </td>

    </tr>
}