【问题标题】:unable to access the linq to sql result in mvc4无法访问 linq to sql 导致 mvc4
【发布时间】:2013-06-14 11:39:39
【问题描述】:

我正在从使用 linq 到 sql 的 theee 表中获取结果,但我无法使用视图中的值,因为我是 mvc 新手,我该如何做到这一点

这是我的控制器代码:

public ActionResult Grid()
{
    ViewBag.Message = "Your Grid page.";

    bc_limsEntities objbc_limsDBContext = new bc_limsEntities();


    var result = from b in objbc_limsDBContext.dc_tpatient_bookingm
                  join d in objbc_limsDBContext.dc_tpatient_bookingd on b.bookingid equals d.bookingid
                  join t in objbc_limsDBContext.dc_tp_test on d.testid equals t.TestId

                  where b.bookingid == 41239 && d.ProcessID == 0006 && t.SubdepartmentId == 16

                  select new {b.bookingid,
                              d.bookingdid,
                              d.testid,
                              t.Test_Name,
                              t.procedureid,
                              t.ClinicalUse,
                              t.AutomatedText};

    ViewBag.Result = result.ToList();


    return View(result);
}

我正在这样做:

@foreach(var item in ViewBag.Result)
{
    <h1>@item</h1>
}

【问题讨论】:

  • 您的数据库上下文没有得到处理。将其包裹在 using 子句中。
  • @asawyer 说得对,我认为

标签: sql-server asp.net-mvc linq razor


【解决方案1】:

我就是这样解决了自己的问题:

public class ResultSet
{
    public long bookingid { get; set; }
    public long bookingdid { get; set; }
    public long testid { get; set; }
    public string Test_Name { get; set; }
    public long procedureid { get; set; }
    public string ClinicalUse { get; set; }
    public string AutomatedText { get; set; }

}

在控制器中:

bc_limsEntities objbc_limsDBContext = new bc_limsEntities();


        var result = from b in objbc_limsDBContext.dc_tpatient_bookingm
                      join d in objbc_limsDBContext.dc_tpatient_bookingd on b.bookingid equals d.bookingid
                      join t in objbc_limsDBContext.dc_tp_test on d.testid equals t.TestId

                      where b.bookingid == 41239 && d.ProcessID == 0006 && t.SubdepartmentId == 16

                      select new {b.bookingid,
                                  d.bookingdid,
                                  d.testid,
                                  t.Test_Name,
                                  t.procedureid,
                                  t.ClinicalUse,
                                  t.AutomatedText};

        List<ResultSet> resultList = new List<ResultSet>();

        foreach (var temp in result)
        {
            ResultSet objResultSet = new ResultSet();

            objResultSet.bookingid = temp.bookingid;
            objResultSet.bookingdid = temp.bookingdid;
            objResultSet.testid = temp.testid;
            objResultSet.Test_Name = temp.Test_Name;
            objResultSet.procedureid = long.Parse(temp.procedureid.ToString());
            objResultSet.ClinicalUse = temp.ClinicalUse;
            objResultSet.AutomatedText = temp.AutomatedText;

            resultList.Add(objResultSet);
        }

        ViewBag.Result = resultList;
return View(resultList);

在这样的视图中:

@foreach(var item in Model)
          {
              <tr>
              <td class="t_center"><input type="checkbox" id="c14" name="cc" /><label for="c14"><span></span></label></td>
              <td>@item.bookingid</td>
              <td>@item.bookingdid</td>
              <td>@item.testid</td>
              <td>@item.Test_Name</td>
              <td>@item.procedureid</td>
              <td>@item.ClinicalUse</td>
              <td>@item.AutomatedText</td>

              </tr>                  
          }

【讨论】:

    【解决方案2】:

    不要使用视图包,使用视图模型。在这种情况下,您已经将 results 作为视图模型传递,因此您需要在视图页面的顶部声明模型,如下所示:

    @model List<Item>
    

    (或任何类型的对象 results 是)

    那么你可以这样使用它:

    @foreach(var item in Mode)
    {
        <h1>@item.Property</h1>
    }
    

    通常,您会有一个专用的 View Model 类,其中包含许多属性,其中之一是您的 results 列表,如果您喜欢冒险,请尝试使用它!

    编辑:因为您正在动态创建一个您不会知道类型的自定义对象,所以您应该创建一个包含您需要的所有字段的类(例如Item),然后从您的三个表中填充该类,然后使用它。

    【讨论】:

    • 我同意。您应该创建可以用注释装饰的视图模型类型并将您的视图绑定到这些类型。
    • 通过这种方式,我的视图将被填充,但如果单击特定项目,我将如何获得预订 ID? @asawyer
    • @EhsanSajjad:如果您想将预订 ID 放在某处,请使用 @item.bookingid,但这确实是一个不同的问题
    【解决方案3】:

    试试类似的东西怎么样:

    @foreach(var item in ViewBag.Result)
    {
        <h1>@item.bookingid</h1>
    }
    

    如果您当然想坚持使用 viewbag,但 musefan 关于使用视图模型的建议更简洁,您应该真正做些什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-19
      • 1970-01-01
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 2015-11-03
      相关资源
      最近更新 更多