【问题标题】:Html.Action not workHtml.Action 不起作用
【发布时间】:2015-06-04 07:51:19
【问题描述】:

我想将问题链接列表的部分视图带到我的编辑视图中。但是,当我选择 groupby 值时,它会显示以下错误。请指导我。

传入字典的模型项是类型 'System.Collections.Generic.List1[<>f__AnonymousType42[SurveyTool.Models.SURV_Question_Ext_Model,SurveyTool.Models.SURV_Question_Model]]', 但是这本字典需要一个类型的模型项 '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>

【问题讨论】:

    标签: c# linq asp.net-mvc-4 view


    【解决方案1】:

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

    @model IEnumerable<SurveyTool.Models.SURV_Question_Ext_Model>
    

    到:

    @model List<SurveyTool.Models.SURV_Question_Ext_Model>
    

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

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

    【讨论】:

    • 我已更改为 List 但仍然相同。没有运气。
    • 还返回了一组LINQ查询,所以它是异常类型,而不是您期望的类型。你试图传递给你的视图是什么?它是该组的密钥集合吗?还是第一组值?
    • var query = from r in db.SURV_Question_Ext_Model join s in db.SURV_Question_Model on r.Qext_Question_ID 等于 s.Question_ID 其中 s.Question_Survey_ID == Survey_ID group new { r, s } by r.Qext_Language into grp select grp.FirstOrDefault();
    • select grp 在这种情况下返回包含 Key 对象和值集合的 Group。并且整个查询返回 grp 项目的集合,因此在异常中给出匿名类型,
    • 我想加入表并从 2 个表中获取值。这是来自 SURV_Question_Ext_Model 和 SURV_Question_Model 的数据。
    【解决方案2】:

    你有一点错误。试试这个:

    @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>
    }
    

    【讨论】:

    • 我看不出有什么不同。
    • 也许这就是原因:@Html.ActionLink("QuestionLink", "Edit", "SURV_Answer", new { Language = item.Qext_Language }, null) -> @Html.ActionLink("编辑”、“QuestionLink”、“SURV_Answer”、新 { Language = item.Qext_Language }, null)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-30
    相关资源
    最近更新 更多