我的视图不在mvc中显示我的表数据

时间:2018-11-23 10:01:50

标签: c# entity-framework model-view-controller

有一个小问题,可能是一个简单的解决方案,但无法解决!

我无法在视图中显示数据。我有5个复选框,我想显示属于每个复选框的所有数据。当我在视图中将数据硬编码到列表中时,此复选框有效,但是当我想从表中显示数据时,该复选框确实起作用了,

我的代码

我的模型(使用实体框架从我的表中创建)

 public partial class Full
{
    public int Id { get; set; }
    public string Channel { get; set; }
    public string Program { get; set; }
    public string Category { get; set; }
    public string Date { get; set; }
    public string Time { get; set; }
    public string Length { get; set; }
}

我的观点

@model IEnumerable<Uppgift4.Models.Full>

@{
    ViewBag.Title = "channel_Index";
    var list = Model.ToList();
    var list1 = list.Where(_ => _.Channel == "SVT1").Select(_ => 
    _.Program).ToList();//select records which in one channel
    var list2 = list.Where(_ => _.Channel == "SVT2").Select(_ => 
    _.Program).ToList();
    var list3 = list.Where(_ => _.Channel == "TV3").Select(_ => 
   _.Program).ToList();
    var list4 = list.Where(_ => _.Channel == "TV4").Select(_ => 
   _.Program).ToList();
    var list5 = list.Where(_ => _.Channel == "Kanal5").Select(_ => 
   _.Program).ToList();

 }
<style>
.hiddenRow {
    padding: 0 !important;
}

.in-line {
    display: inline;
}
</style>
<br />
<table class="table table-condensed" style="border-collapse:collapse;">
<thead>
    <tr>
        <th>
            Channel
        </th>
        <th>

        </th>
    </tr>
</thead>
<tbody>
<tr class="accordion-toggle">
        <td><input type="checkbox" data-toggle="collapse" data-target="div   
       [id*='demo1']" /> SVT1</td>
    </tr>
    @for (var i = 0; i < @list1.Count; i++)
    {
        <tr><td colspan="2" class="hiddenRow"><div id="demo1+'@i'+" 
        class="accordian-body collapse">Program:@list1[i]</div> </td> </tr>

    }
 <tr data-toggle="collapse" data-target="#demo2" class="accordion-toggle">
        <td><input type="checkbox" data-toggle="collapse" data-target="div
        [id*='demo2']" /> SVT2</td>
    </tr>
    @for (var i = 0; i < @list2.Count; i++)
    {
        <tr><td colspan="2" class="hiddenRow"><div id="demo2+'@i'+" 
        class="accordian-body collapse">Program:@list2[i]</div> </td> </tr>

    }

    <tr data-toggle="collapse" data-target="#demo3" class="accordion-toggle">
        <td><input type="checkbox" data-toggle="collapse" data-target="div
        [id*='demo3']" /> TV3</td>
    </tr>
    @for (var i = 0; i < @list3.Count; i++)
    {
        <tr><td colspan="2" class="hiddenRow"><div id="demo3+'@i'+" 
        class="accordian-body collapse">Program:@list3[i]</div> </td> </tr>

    }

    <tr data-toggle="collapse" data-target="#demo4" class="accordion-toggle">
        <td><input type="checkbox" data-toggle="collapse" data-target="div
        [id*='demo4']" /> TV4</td>
    </tr>
    @for (var i = 0; i < @list4.Count; i++)
    {
        <tr><td colspan="2" class="hiddenRow"><div id="demo4+'@i'+" 
        class="accordian-body collapse">Program:@list4[i]</div> </td> </tr>

    }

    <tr data-toggle="collapse" data-target="#demo5" class="accordion-toggle">
        <td><input type="checkbox" data-toggle="collapse" data-target="div
        [id*='demo5']" /> Kanal5</td>
    </tr>
    @for (var i = 0; i < @list5.Count; i++)
    {
        <tr><td colspan="2" class="hiddenRow"><div id="demo5+'@i'+" 
        class="accordian-body collapse">Program:@list5[i]</div> </td> </tr>

    }
</tbody>
</table>

我的控制器

public class FavoritChannelsController : Controller
{
    TvProgramDBEntities db = new TvProgramDBEntities();
    Full f = new Full();

    public ActionResult channel_index()
    {
        List<Full> model = new List<Full>();
        {
            model.Add(new Full { Channel = "SVT1", Program = "fame" });
            model.Add(new Full { Channel = "SVT1", Program = "sport" });
            model.Add(new Full { Channel = "SVT2", Program = "news" });
            model.Add(new Full { Channel = "TV3", Program = "hockey" });
            return View(model);

        }
    }

因此,当我整理列表时,现在执行该程序并按下复选框SVT1时,此动作结果起作用。

但是我想显示我表中的所有数据,所以我尝试这样做:

 public ActionResult channel_index()
    {
        List<Full> model = new List<Full>();
        foreach (var item in db.Full)
        {
            f.Channel = item.Channel;
            f.Program = item.Program;
            model.Add(f);
        }
        return View(model);
    }

但是运行程序时,我的视图中没有任何数据。

有人知道解决方案吗?

3 个答案:

答案 0 :(得分:0)

问题是,如果您写b.Full,则会得到IQueryable<Full>,但如果要执行查询,则应写.toList()

已更新:

在数据库中,您有Program,其中包含很多空格,这意味着"SVT1" != "SVT1 "。您需要使用Regex.Replace(text, @"s", "")

您可以看到另一个示例,该示例介绍如何删除空格http://www.csharp411.com/remove-whitespace-from-c-strings/

public ActionResult channel_index()
        {
            List<Full> model = new List<Full>();
            //you need to do the query with ToList
            var list = db.Full.ToList();

            foreach (var item in list )
            {
                var initedF = new Full({
                   Channel = Regex.Replace(item.Channel, @"s", ""), 
                   Program = Regex.Replace(item.Program, @"s", "")
                });
                //f.Channel = item.Channel;
                //f.Program = item.Program;
                model.Add(initedF);
            }
            return View(model);
        }

答案 1 :(得分:0)

CREATE TABLE [dbo].[Full] (
[Id]       INT        IDENTITY (1, 1) NOT NULL,
[Channel]  NCHAR (50) NULL,
[Program]  NCHAR (50) NULL,
[Category] NCHAR (50) NULL,
[Date]     NCHAR (50) NULL,
[Time]     NCHAR (50) NULL,
[Length]   NCHAR (10) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);

答案 2 :(得分:0)

好的,这就是我应该在控件中写的内容,它确实起作用了。在新的Full之后,()出现了错误,所以我把它拿走了,这可能是问题吗?

 public ActionResult channel_index()
    {
        List<Full> model = new List<Full>();
        //you need to do the query with ToList
        var list = db.Full.ToList();

        foreach (var item in list)
        {
            var initedF = new Full
            {
                Channel = item.Channel,
                Program = item.Program
            };
            //f.Channel = item.Channel;
            //f.Program = item.Program;
            model.Add(initedF);
        }
        return View(model);
    }