简体   繁体   中英

group by value with 2 different value and show in Razor view

i have 2 Question list and 1 of the question have 2 language, i will like to show 2 Action Link languages for that question row if it have. But i have no idea how to show the another language in a row. I only able to show one language.

Any Hint or guidance?

db.SURV_Question_Model data:

__________________________________________
Survey ID |    Question_ID    | Qext_Text | 
==========================================
1         |       1           |  Q1       |          
2         |       2           |  Q2       |               
==========================================

db.SURV_Question_Ext_Model data:

___________________________________________________
| Quext_Question_ID | Qext_Text | Qext_Language |
===================================================
|       1           |  Q1       |     ENG       |
|       1           |  Q1       |     GER       |
|       2           |  Q2       |     ENG       |
===================================================

What i want to show in Razor view :

__________________________________________________
Quext_Question_ID | Qext_Text | Qext_Language     |
===================================================
       1           |  Q1       |     ENG | GER    |
       2           |  Q2       |     ENG          |
===================================================

My Controller:

public ActionResult SURV_Main_Details_QuestionList(int Survey_ID)
        {
            List<QuestionLinkListViewModel> viewmodel = new List<QuestionLinkListViewModel>();

            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_Question_ID into grp
                        select grp.FirstOrDefault();

            foreach (var item in query)
            {
                viewmodel.Add(new QuestionLinkListViewModel()
                {
                    Survey_ID = Survey_ID,
                    Question_ID = item.r.Qext_Question_ID,
                    QuestionText = item.r.Qext_Text,
                    Languages = item.r.Qext_Language,
                });

            }

            return PartialView(viewmodel);
        }

My Razor View:

@using (Html.BeginForm())
{
    <table class="strip">

        for (int i = 0; i < Model.Count(); i++)
        {
            <tr>
                <td></td>

                <td width="70%">
                    @Model[i].QuestionText
                </td>
                <td>
                    @Html.ActionLink(Model[i].Languages, "SURV_Question_Edit", "SURV_Question", new { Question_ID = Model[i].Question_ID, Languages = Model[i].Languages }, new { @class = "popup-link2" }) 
                    @Html.Raw(" ")
                    @Html.ActionLink(Model[i].Languages2, "SURV_Question_Edit", "SURV_Question", new { Question_ID = Model[i].Question_ID, Languages = Model[i].Languages2 }, new { @class = "popup-link2" }) 
                </td>

            </tr>
            }

    </table>
}

i fixed it by adding Query inside a query:

public ActionResult SURV_Main_Details_QuestionList(int Survey_ID)
        {
            List<QuestionLinkListViewModel> viewmodel = new List<QuestionLinkListViewModel>();

            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_Question_ID into grp
                        select grp.FirstOrDefault();

            foreach (var item in query.ToList())
            {
                var queryLang = 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 r.Qext_Question_ID == item.r.Qext_Question_ID
                                select s;

                if (queryLang.Count() == 1)
                { 
                    viewmodel.Add(new QuestionLinkListViewModel()
                    {
                        Survey_ID = Survey_ID,
                        Question_ID = item.r.Qext_Question_ID,
                        QuestionText = item.r.Qext_Text,
                        Languages = item.r.Qext_Language,
                        Languages2 = " " 
                    });
                }
                else if(queryLang.Count() == 2)
                {
                    viewmodel.Add(new QuestionLinkListViewModel()
                    {
                        Survey_ID = Survey_ID,
                        Question_ID = item.r.Qext_Question_ID,
                        QuestionText = item.r.Qext_Text,
                        Languages = "ENG",
                        Languages2 = "GER"
                    });

                }

            }

            return PartialView(viewmodel);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM