asp.net-mvcinner-joinviewmodelef-database-firstnested-select

How Can I Create two new fields in a list by filtering the one specific field in MVC?


I have a ViewModel as:

public class ApplicationContentViewModel
{
    public BPMSPARS.Models.MySql.application application {get; set;}
    public BPMSPARS.Models.MySql.content content { get; set; }
    public BPMSPARS.Models.MySql.app_delegation app_delegation { get; set; }
}

On the Other hand, I want to create a list with this out put in View:

@foreach (var item in Model)
{
    i++;
    <tr>
        <td>@i</td>
        <th>
           @item.content.CON_VALUE
           //where CATEGORY="TASK"
        </th>
        <th>
            @item.content.CON_VALUE
            //where CATEGORY="PRO_TITLE"
        </th>
        <th>@item.application.APP_CREATE_DATE</th>
        <th>@User.Identity.Name.Split('-')[0]</th>
    </tr>
}

In fact I want to display one field in two column with different condition ( @item.content.CON_VALUE).

First, I use this query,But it can't return a first @item.content.CON_VALUE and I can't apply two condition for one field(in two column).

public ActionResult ProcessList()
{ 
   var db1 = new wf_workflowEntities();
   ApplicationContentViewModel APVM = new ApplicationContentViewModel();

   var ViewModel = (from APP in db1.application
                    join app_del in db1.app_delegation on APP.APP_UID equals app_del.APP_UID
                    join Con in db1.content on APP.PRO_UID equals Con.CON_ID
                    where Con.CON_ID == APP.PRO_UID && app_del.APP_UID == APP.APP_UID && Con.CON_CATEGORY == "PRO_TITLE"
                    select new ApplicationContentViewModel
                    {
                        app_delegation = app_del,
                        application = APP,
                        content = Con,
                    }).AsEnumerable();

    return View(ViewModel);
}

Do you know a suitable solution with this ?


Solution

  • If I understand this right, you should remove this condition from your query:

    && Con.CON_CATEGORY == "PRO_TITLE"
    

    That way it will select both PRO_TITLE and TASK. Then in your cshtml:

       <th>
           @(item.content.CON_CATEGORY=="TASK"?item.content.CON_VALUE:"")
        </th>
        <th>
            @(item.content.CON_CATEGORY=="PRO_TITLE"?item.content.CON_VALUE:"")
        </th>
    

    This will display CON_VALUE in the first column when the category is TASK (else blank), and it will display CON_VALUE in the second column if it is PRO_TITLE (else blank).