【问题标题】:How to pass join sql query table result to another method?如何将join sql查询表结果传递给另一种方法?
【发布时间】:2016-08-05 11:52:51
【问题描述】:

我正在使用实体框架在 ASP.NET MVC 中工作。我的数据库中有两个表,AppointmentDbStudentDb。我希望将它们加入控制器并将结果传递给控制器​​中的另一个操作方法。我在Details 操作中传递的这个新表的返回类型是什么?

public ActionResult Index(AdminViewModel model)
{
    if (ModelState.IsValid)
    {              
        var innerJoinQuery = from appointment in AppointmentDb
                             join student in StudentDb on appointment.StudentFirstName equals student.FirstName
                             select appointment;
        return View("Details",innerJoinQuery);
    }
}

public ActionResult Details(?????? innerJoinQuery)
{           
    return View();
}

编辑 1:

根据下面的答案,我编辑为:

    public ActionResult Details(IQueryable<Appointment> appointment)
    {

        return View(appointment);
    }

我仍然无法根据需要调用下面的列。其中一些像AppointmentID 来自AppointmentDb,其余来自StudentDb 下面是Details.cshtml的视图:

@model IQueryable<OdeToFood.Entities.Appointment>

@{
    ViewBag.Title = "Details";
}

<h2>Index</h2>
@Html.ActionLink("Back to Search", "Index")
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.AppointmentID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.StudentFirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.StudentID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.AppointmentDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.AppointmentTime)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Status)
        </th>
    </tr>
</table>

我应该如何使View 工作?

编辑 2:

我已按照编辑 1 中的建议进行了编辑。我的完整 View.cshtml 中还有另一个小错误。这是我收到的错误foreach cannot operate on Variables of type OdeToFood.ViewModels.AdminDetailsViewModel because it does not contain definition for GetEnumerator

@model AdminDetailsViewModel

@{
    ViewBag.Title = "Details";
}

<h2>Index</h2>
@Html.ActionLink("Back to Search", "Index")
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.AppointmentID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.StudentFirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.StudentLastName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.StudentID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.AppointmentDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.AppointmentTime)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Status)
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.AppointmentID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.StudentFirstName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.StudentLastName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.StudentID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.AppointmentDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.AppointmentTime)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Status)
            </td>
        </tr>
    }

</table>

【问题讨论】:

  • 尝试对象或动态
  • 自己找出这一点的一种方法是将参数类型指定为 int 或 string,然后查看编译器消息显示的内容。
  • 这是讨厌 var 被滥用的原因之一,这使得大多数开发人员甚至不会考虑他们正在使用的对象的类型。

标签: c# mysql asp.net asp.net-mvc


【解决方案1】:

您的查询代码返回IQueryable&lt;AppointmentDb&gt;

 public ActionResult Details(IQueryable<AppointmentDb> innerJoinQuery)
 {           
        return View();
 }

方法二:

最好先执行你的查询:

var innerJoinQuery = (from appointment in AppointmentDb
                     join student in StudentDb 
                     on appointment.StudentFirstName equals student.FirstName
                     select appointment).ToList();

然后传递结果列表:

public ActionResult Details(List<AppointmentDb> innerJoinQuery)
{           
    return View();
}

编辑:

您需要创建一个类来保存来自多个表的数据:

class MyViewModel
{
    public int AppointmentID { set; get; }
    public string StudentFirstName { set; get; }
    // continue the rest
}

然后将您的查询更改为:

from appointment in AppointmentDb
join student in StudentDb
on appointment.StudentFirstName equals student.FirstName
select new MyViewModel()
{
    AppointmentID = AppointmentDb.AppointmentID,
    StudentFirstName = StudentDb.StudentFirstName
    // continue the rest
};

【讨论】:

  • @stuartd 你为什么和IQueryable在一起?
  • @re3el 它必须是 IQueryable&lt;AdminDetailsViewModel&gt; 或使用 foreach 的列表。不是一个单一的对象。
  • 它不适用于IQueryable&lt;AdminDetailsViewModel&gt;,但IEnumerable&lt;AdminDetailsViewModel&gt; 消除了错误
  • @re3el 这一切都取决于您传递的内容。然后使用IEnumerable。或者更好地使用答案中指定的.ToList() 执行查询,然后在视图中使用List&lt;AdminDetailsViewModel&gt;
  • @user3185569 你能看看这个 [stackoverflow.com/questions/38786074/…
【解决方案2】:

你可以使用对象类型

public ActionResult Details(object innerJoinQuery)
{           
   return View();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-24
    • 2021-02-04
    • 1970-01-01
    • 2017-07-29
    • 2018-12-24
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    相关资源
    最近更新 更多