【问题标题】:SQL Query only returns one row , when I try Get All rows from table当我尝试从表中获取所有行时,SQL 查询只返回一行
【发布时间】:2017-05-08 10:25:54
【问题描述】:

我正在使用 MVC 5,并且我有一个页面,您可以在其中更新博客,但是当我尝试获取所有行或从表中获取所有博客(相对于更新博客)时,它只返回第一行,甚至我有 4 行表格。有人可以指出我正确的方向吗?

The Fac 中的代码:

 public class AutoFac<T> where T : new()
 {
private string table;
private Mapper<T> mapper = new Mapper<T>();

public T GetAlle()
        {
using (var cmd = new SqlCommand("SELECT * FROM " + table, Conn.CreateConnection()))
            {
                var r = cmd.ExecuteReader();
                T type = new T();

                if (r.Read())
                {
                    type = mapper.Map(r);
                }

                r.Close();
                cmd.Connection.Close();
                return type;
            }

        }
}


控制器中的代码:

public ActionResult UpdateBlog()
        {
            return View(bf.GetAlle());
        }

        [HttpPost]
        public ActionResult UpdateBlog(Blog b)
        {

            bf.Update(b);
            ViewBag.MSG = "Blog is updated now";
            return View();
        }


视图中的代码:

@model BlogRep.Blog

@{
    ViewBag.Title = "UpdateBlog";
}

<h2>UpdateBlog</h2>

@using (Html.BeginForm())
{
    <div class="form-horizontal">

        <h4>Blog</h4>
        <hr />

        @Html.HiddenFor(model =>model.ID)

        <div class="form-group">
            @Html.LabelFor(model => model.Overskrift, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Overskrift, new { htmlAttributes = new { @class = "form-control" } })

            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Tekst, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Tekst, new { htmlAttributes = new { @class = "form-control" } })

            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Dato, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Dato, new { htmlAttributes = new { @class = "form-control" } })

            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />

            </div>
            @ViewBag.MSG
        </div>

    </div>

}

【问题讨论】:

标签: sql-server asp.net-mvc


【解决方案1】:

Fac中的代码应该是这样的

public class AutoFac<T> where T : new()
{
    private string table;
    private Mapper<T> mapper = new Mapper<T>();

    public T[] GetAlle()
    {
        using (var cmd = new SqlCommand("SELECT * FROM " + table, Conn.CreateConnection()))
        {
            var r = cmd.ExecuteReader();
            List<T> type = new List<T>();

            while(r.Read())
            {
                type.Add(mapper.Map(r));
            }

            r.Close();
            cmd.Connection.Close();
            return type.ToArray();
        }

    }
}

你可以像这样改变你的看法

@model BlogRep.Blog[]

@{
    ViewBag.Title = "UpdateBlog";
}

<h2>AllBlogs</h2>


    <div class="col-md-12">
        <table>
            <thead>
                <tr>
                    <th class="headeralign" data-sortable="true">Overskrift</th>
                    <th class="headeralign" data-sortable="true">Tekst</th>
                    <th class="headeralign" data-sortable="true">Dato</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                        <td align="center">@item.Overskrift</td>
                        <td align="center">@item.Tekst</td>
                        <td align="center">@item.Dato</td>
                    </tr>
                }
            </tbody>
        </table>
    </div>

【讨论】:

  • tnx 给你评论,我是否也应该改变我的看法! ,当我使用 List 是正确的吗? bcz,我只是更改了 fac,然后我得到了这个错误:传递给字典的模型项的类型是“BlogRep.Blog []”,但是这个字典需要一个类型为“BlogRep.Blog”的模型项。
  • 是的,您还需要更改视图,因为您将获得数组
  • 我刚刚使用了 foreach ,但仍然返回一行!
  • 再一次,它只返回一行:(
  • 不,伙计,它应该给出所有记录的列表,检查数据库一次
猜你喜欢
  • 1970-01-01
  • 2021-12-07
  • 2016-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-05
  • 1970-01-01
相关资源
最近更新 更多