在视图中显示SQL查询结果

时间:2021-02-13 19:37:25

I'm new to MVC 5 with .net

我对。net的MVC 5很熟悉。

Basically I'm just trying to get my bearings and I want to display some generic queries (disregard the fact that im using the master db, I just want to get the functions working right now). I'm using the authentication 2.0 which has an applicatindbcontext, but I created my own context but since I'm not really wanting to create a model (which could be the problem) I didn't know what to create for properties:

基本上,我只是试图了解我的方位,我想显示一些通用的查询(不考虑我使用主db,我只想让函数立即工作)。我使用的是有applicatindbcontext的认证2.0,但是我创建了我自己的上下文,但是因为我并不想创建一个模型(这可能是个问题),所以我不知道为属性创建什么:

public class MasterModel : DbContext
{
    public MasterModel() : base("MasterDatabaseConnection")
    { }
}

I created the controller like:

我创建了控制器:

public class MasterController : Controller
{
    private MasterModel db = new MasterModel();
    // GET: Statistics
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult GetVersion()
    {
        string query = "SELECT @@VERSION AS Version";
        IEnumerable<MasterModel> data = db.Database.SqlQuery<MasterModel>(query);

        return View(data.ToList());
    }
}

And finally I'm trying to figure out how to display the results in the view...and I'm completely lost (although it's possible I was lost in one of the previous steps).

最后,我想知道如何在视图中显示结果……我完全迷失了方向(尽管我可能在前面的步骤中迷失了方向)。

@model IEnumerable<IdentitySample.Models.MasterModel>

@{
    ViewBag.Title = "Index";
}

@WTF.Am.I.SupposedToPutHere

I've followed some tutorials where I've created CRUD style model view controllers, but I guess I'm not drawing the connection on how to just submit informational queries and display the results.

我已经在一些教程中创建了CRUD样式的模型视图控制器,但是我想我并没有画出如何提交信息查询和显示结果的连接。

1 个解决方案

#1


1  

Create a Context

创建一个上下文

public class MasterModel : DbContext
{
    public MasterModel() : base("MasterDatabaseConnection")
    { }
    public DbSet<MyModel> ModelOBJ { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
      modelBuilder.Entity<ModelOBJ>().ToTable("tblModelOBJ");
    }
}

Create a Model

创建一个模型

Public cLass MyModel { public int ID {get;set;} public string Name {get;set;} } Public cLass MyModelRepository {

Public类MyModel {Public int ID {get;set;} Public string Name {get;set;}

   public List<MyModel> GetALlModelFromDB()
   {
     MasterModel md = new MasterModel();
     return md.ModelTosend.toList(); 
   }
}

In your Controller

在你的控制器

 public ActionResult Index()
    {
        return View(new MyModelRepository().GetALlModelFromDB());
    }

In your View

在你的观点

@model IEnumerable<IdentitySample.Models.MyModel>

@{
    ViewBag.Title = "Index";
}

@foreach(var item in Model)
{
   @:<div>@item.ID   @item.Name </div>
}

#1


1  

Create a Context

创建一个上下文

public class MasterModel : DbContext
{
    public MasterModel() : base("MasterDatabaseConnection")
    { }
    public DbSet<MyModel> ModelOBJ { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
      modelBuilder.Entity<ModelOBJ>().ToTable("tblModelOBJ");
    }
}

Create a Model

创建一个模型

Public cLass MyModel { public int ID {get;set;} public string Name {get;set;} } Public cLass MyModelRepository {

Public类MyModel {Public int ID {get;set;} Public string Name {get;set;}

   public List<MyModel> GetALlModelFromDB()
   {
     MasterModel md = new MasterModel();
     return md.ModelTosend.toList(); 
   }
}

In your Controller

在你的控制器

 public ActionResult Index()
    {
        return View(new MyModelRepository().GetALlModelFromDB());
    }

In your View

在你的观点

@model IEnumerable<IdentitySample.Models.MyModel>

@{
    ViewBag.Title = "Index";
}

@foreach(var item in Model)
{
   @:<div>@item.ID   @item.Name </div>
}