使用SQL(实体框架)中的数据填充表或列表

时间:2015-10-07 07:35:56

标签: c# asp.net-mvc entity-framework

我无法获取数据到我的表/列表,我试图关注几个视频和堆栈溢出的答案,但没有数据显示在表格中......

  

这是我的" Klubovi" class," KluboviContext" class和" KluboviController":

" Klubovi" class," KluboviContext"课程在模特中。

using System; 
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace filltabletest.Models
{
    public class Klubovi
    {
        public virtual int Id { get; set; }
        public virtual string ImeKluba { get; set; }
        public virtual string KratkoIme { get; set; }
        public virtual string Sport { get; set; }
        public virtual string League { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using filltabletest.Models;
using System.Data.Entity;

namespace filltabletest.Models
{
    public class KluboviContext : DbContext
    {
        public DbSet<Klubovi> KluboviPkg { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using filltabletest.Models;

namespace filltabletest.Controllers
{
    public class KluboviController : Controller
    {
        //
        // GET: /Klb/
        private KluboviContext db = new KluboviContext();

        public ActionResult Index()
        {
            var KluboviItems = db.KluboviPkg;
            return View(KluboviItems.ToList());
        }

    }
}

查看:

@model IEnumerable<filltabletest.Models.Klubovi>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml"; }

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create") </p> <table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.ImeKluba)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.KratkoIme)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Sport)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.League)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.ImeKluba)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.KratkoIme)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Sport)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.League)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr> }

<connectionStrings>
    <add name="KluboviContext" connectionString="Data Source=ZWERINJAK;Initial Catalog=OkladeNEW2;User ID=sa;Password=sa" providerName="System.Data.SqlClient" />
</connectionStrings>

Klubovi Db: Image:Klubovi db

结果: Image: Result from code

有人可以帮我解决一下该怎么办?

0 个答案:

没有答案