我正在使用MVC创建的论坛,我无法使用我的部分视图添加表格数据。
我的“ShowPost”视图如下所示(当您使用实体框架创建具有读/写功能的控制器并且将模型设置为我的'Post'表时,自动生成的视图)
@model IEnumerable<Forum6.Models.Post>
@{
ViewBag.Title = "ShowPost";
}
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.topicid)
</th>
<th>
@Html.DisplayNameFor(model => model.date)
</th>
<th>
@Html.DisplayNameFor(model => model.text)
</th>
<th>
@Html.DisplayNameFor(model => model.userid)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Topics.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.date)
</td>
<td>
@Html.DisplayFor(modelItem => item.text)
</td>
<td>
@Html.DisplayFor(modelItem => item.Users.username)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.postid }) |
@Html.ActionLink("Details", "Details", new { id=item.postid }) |
@Html.ActionLink("Delete", "Delete", new { id=item.postid })
</td>
</tr>
} @* End loop *@
@* My Partial view *@
<div id="panel">@Html.Partial("_CreatePost", new Forum6.Models.Post())</div>
<br />
以下是我的部分视图“_CreatePost”中的代码
@model Forum6.Models.Post
@{
ViewBag.Title = "CreatePost";
}
<h2>CreatePost</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Post</legend>
<div class="editor-label">
@Html.LabelFor(model => model.date)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.date)
@Html.ValidationMessageFor(model => model.date)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.text)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.text)
@Html.ValidationMessageFor(model => model.text)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.userid)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.userid)
@Html.ValidationMessageFor(model => model.userid)
</div>
<p>
<input type="submit" value="AddData" />
</p>
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
以下是该视图的屏幕截图
编辑: HomeController中的ShowPost:
public ActionResult ShowPost(int id = 0)
{
// 19.03.13
var posts = (from p in db.Post
where p.topicid == id
select p);
ViewBag.currentTopic = id;
return View(posts);
}
在HomeController中创建Post方法
public ActionResult CreatePost(int id = 0)
{
ViewBag.topicid = new SelectList(db.Topics, "topicid", "name");
ViewBag.userid = new SelectList(db.Users, "userid", "username");
ViewBag.currentTopic = id;
return View();
}
[HttpPost]
public ActionResult CreatePost(Post post, int id = 0)
{
// Set TopicId and Date
post.topicid = id;
//08.04.2013
post.date = DateTime.Now;
if (ModelState.IsValid)
{
db.Post.Add(post);
db.SaveChanges();
return RedirectToAction("ShowPost", new { id = post.topicid });
}
ViewBag.topicid = new SelectList(db.Topics, "topicid", "name", post.topicid);
ViewBag.userid = new SelectList(db.Users, "userid", "username", post.userid);
return View(post);
}
单击“AddData”时,字段清除,但不添加任何数据。我不知道从哪里开始。
谁能看到我出错的地方?