C#查询返回视图

时间:2014-08-21 16:23:38

标签: c# sql razor

我试图在视图中显示在IMPORT_TRACKER表上执行的查询的结果。我返回视图的方法不起作用。

ImportController.cs

public ActionResult ImportSelection()
    {
        {
                string db = "Data Source=SWDB10DSQL;Initial Catalog=BillingUI;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework";
                string query = "SELECT MAX(IMPORT_DATE) FROM IMPORT_TRACKER;";
                var result = db.IMPORT_TRACKER(query);
                return View(result);
        }
    }

ImportSelection.cshtml

@model IEnumerable<BillingApp.Models.IMPORT_TRACKER>

@{
ViewBag.Title = "Import Selection";

}

@foreach (var item in Model)
       {
           <div>@item</div>
       }

模型(IMPORT_TRACKER.cs):

namespace BillingApp.Models
{
using System;
using System.Collections.Generic;

public partial class IMPORT_TRACKER
{
    public int ID { get; set; }
    public string IMPORT_DATE { get; set; }
    public string T1_1STATUS { get; set; }
    public string T1_2STATUS { get; set; }
    public string T1_3STATUS { get; set; }
    public string T1_4STATUS { get; set; }
    public string T1_5STATUS { get; set; }
    public string T1_6STATUS { get; set; }
    public string T1_7STATUS { get; set; }
    public string T2_1STATUS { get; set; }
    public string T2_2STATUS { get; set; }
    public string T2_3STATUS { get; set; }
    public string T2_4STATUS { get; set; }
    public string T2_5STATUS { get; set; }
    public string T3STATUS { get; set; }
    public string T4STATUS { get; set; }
    public string T5STATUS { get; set; }
    public string T6STATUS { get; set; }
    public string T7STATUS { get; set; }
    public string T8STATUS { get; set; }
    public string T9_1STATUS { get; set; }
    public string T9_2STATUS { get; set; }
    public string T10STATUS { get; set; }
}
}

将控制器更改为

 public ActionResult ImportSelection()
    {
        {
            string db = "Data Source=SWDB10DSQL;Initial Catalog=BillingUI;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework";
            string query = "SELECT MAX(IMPORT_DATE) FROM IMPORT_TRACKER;";
            var result = db.IMPORT_TRACKER(query);
            return View("ImportSelection", result);
         }
     }

我收到以下错误&#34;错误3&#39;字符串&#39;不包含&#39; IMPORT_TRACKER&#39;的定义没有扩展方法&#39; IMPORT_TRACKER&#39;接受类型&#39; string&#39;的第一个参数。可以找到(你错过了使用指令或汇编引用吗?)&#34;

1 个答案:

答案 0 :(得分:-1)

我认为您忘记了一个参数,您希望返回结果的结果:

public ActionResult ImportSelection()
    {
        {
                string db = "Data Source=SWDB10DSQL;Initial Catalog=BillingUI;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework";
                string query = "SELECT MAX(IMPORT_DATE) FROM IMPORT_TRACKER;";
                var result = db.IMPORT_TRACKER(query);
                return View("~/Views/ImportSelection.cshtml", result);
        }
    }

另外我认为在视图中你可能想要包装部分代码如下:

@using (Html.BeginForm())
{
    foreach (var item in Model)
    {
        <div>@item</div>
    }
}

希望这会对你有所帮助。