【发布时间】: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>
}
【问题讨论】: