使用视图模型并将查询传递到同一视图

时间:2019-02-18 06:43:40

标签: c# razor

我是mvc架构的新手。我想创建一个视图,该视图具有一个将数据存储到db的表单以及一个分区以使用查询显示详细信息。该视图使用viewmodel- Add_session_ViewModel。

问题是,如果我包括要查看的viewmodel,则显示部分中有错误,如果我包括列表,则窗体中有错误。 代码如下:

控制器:

public ActionResult Add_session()
    {





        //display data


        var query =( from a in db.Session_details_feedback
                    join b in db.Employee_Details_Feedback on a.Trainer_id equals b.Emp_id
                    select new
                    {

                        a.Session_date,
                        a.Session_name,
                        b.Emp_name
                    } ).ToList();
        foreach (var item in query)
        {
          List<Add_session_ViewModel> sessionList = new List<Add_session_ViewModel>
            {
              new Add_session_ViewModel { Session_name=item.Session_name,Session_date=item.Session_date,emp_name=item.Emp_name}


          };
            ViewData.Model = sessionList;



        return View(ViewData.Model);
    }

VIEWMODEL:

public class Add_session_ViewModel : DbContext

{
    public string Session_name { get; set; }
    public int Trainer_id { get; set; }
    public System.DateTime Session_date { get; set; }
     public string emp_name { get; set; }
    public IList<Add_session_ViewModel> Session_List { get; set; }


}

查看:

@using (Html.BeginForm("Add_session", "Home", FormMethod.Post, new { @class = "form-horizontal" }))
            {

                <div class="form-group">


                    @Html.TextBoxFor(x => x.Session_name, new { @class = "form-control", placeholder = " Enter Session name" })


                </div>
                <div class="form-group">


                    @Html.TextBoxFor(x => x.Session_date, new { @class = "form-control", placeholder = " Enter Session date" })


                </div>
                <div class="form-group">

                    @Html.DropDownListFor(x => x.Trainer_id, ViewBag.TrainerList as IEnumerable<SelectListItem>, "Select Trainer")

                </div>

                <div class="form-group">
                    <input id="add" type="submit" value="ADD" />
                </div>
            }
        </div>
    </div>

</center>
<div class="table-responsive">
    <table class="table">
        <thead>
            <tr>

                <th>Date</th>
                <th>Session</th>
                <th>Trainer</th>

            </tr>
        </thead>

        @foreach (var item in Model)
        {

            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Session_date)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Session_name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.emp_name)
                </td>

            </tr>

        }
    </table>

我一直在寻找解决方案好几天了,仍然没有得到任何解决方案。 如果有人可以给我解决方案,将不胜感激。

谢谢。

我的视图模型是:

public class Add_session_ViewModel 

{
    public string Session_name { get; set; }
    public int Trainer_id { get; set; }
    public System.DateTime Session_date { get; set; }
     public string emp_name { get; set; }
}

the view use all these properties  through a form.

At the same time I need to get the data using the below query and get it displayed on the same view:

 var query =( from a in db.Session_details_feedback
                        join b in db.Employee_Details_Feedback on a.Trainer_id equals b.Emp_id
                        select new
                        {

                            a.Session_date,
                            a.Session_name,
                            b.Emp_name
                        } ).ToList();

我不知道如何将查询和视图模型同时绑定到视图

1 个答案:

答案 0 :(得分:0)

首先

DbContext作为基类删除到您的视图模型中,

public class Add_session_ViewModel
{
    public string Session_name { get; set; }
    public int Trainer_id { get; set; }
    public System.DateTime Session_date { get; set; }
    public string emp_name { get; set; }
    public IList<Add_session_ViewModel> Session_List { get; set; }
}

然后尝试将LINQ查询结果映射到视图模型的直接列表中。

public ActionResult Add_session()
{
    Add_session_ViewModel model = new Add_session_ViewModel(); 

    var result =(from a in db.Session_details_feedback
                join b in db.Employee_Details_Feedback on a.Trainer_id equals b.Emp_id
                select new Add_session_ViewModel   //<= Note here
                {
                     Session_date = a.Session_date,
                     Session_name = a.Session_name,
                     emp_name = b.Emp_name
                }).ToList(); 

    model.Session_List = result;      

    return View(model);    //<= Return model to view instead of "ViewData"
}

然后您的视图必须具有的视图模型

@model FeedBack_Form.Models.Add_session_ViewModel

然后将您的foreach循环更改为

@foreach (var item in Model.Session_List)