@Html.ActionLink("Edit", "EditArticle", new { ArticleID = article.ArticleID })
我通过ArticleID检索文章并返回编辑页面:
public ActionResult EditArticle(Guid ArticleID)
{
AddArticleModel AddArticleModel = new AddArticleModel();
AddArticleModel.Categories = entity.TBL_CATEGORIES.Select(a => a);
AddArticleModel.Article = dbo.SelectArticleById(ArticleID);
return View(AddArticleModel);
}
在此之前没有问题。
在我的编辑页面中,我正在更改文章的某些属性(并非所有属性)。例如,我正在更改标题,内容和更新日期。像这样:
@model DunyaYazilim.Models.AddArticleModel
@{
ViewBag.Title = "EditArticle";
Layout = "~/Views/Shared/_LayoutAuthor.cshtml";
}
@using (Html.BeginForm((string)ViewBag.FormAction, "Author"))
{
@Html.ValidationSummary(true, "Makale gönderilirken bir hata oluştu. Lütfen daha sonra tekrar deneyin.")
<div>
<div class="label_header">@Html.Label("Kategori Seçiniz:")</div>
<div>@Html.DropDownList("CategoryID", new SelectList(Model.Categories, "CategoryID", "Name"),Model.Article.TBL_CATEGORIES.Name)</div>
<div class="label_header">@Html.Label("Makale Başlık:")</div>
<div>@Html.TextBoxFor(m => m.Article.Title, new { @class = "my_textbox" })</div>
<div class="label_header">@Html.Label("Makale Açıklama:")</div>
<div>@Html.TextAreaFor(m => m.Article.Description, new { @class = "my_textarea" })</div>
<div class="label_header">@Html.Label("Makale İçerik:")</div>
<div>@Html.TextAreaFor(m => m.Article.ArticleContent, new { @class = "my_textarea" })</div>
<div><input type="submit" value="Gönder" class="my_button" /></div>
</div>
}
然后我发布到:
[HttpPost]
public ActionResult EditArticle(AddArticleModel AddArticleModel, String CategoryID)
{
//TODO: update database...
return View(AddArticleModel);
}
但是未更改的属性返回null(ArticleID,UserID等)。因此我无法更新数据库,因为我在发布后没有ArticleID。这是什么原因?
感谢。
答案 0 :(得分:2)
MVC不会在请求之间为您维护任何内容。当您发布到您的操作时,它将仅发布您在表单中设置的值。由于您在表单中没有您的文章ID或用户ID(或其他任何地方,例如路由或查询字符串),MVC将不会在您的EditArticle操作的模型绑定期间了解它们。
如果您希望通过帖子发送额外的详细信息,可以在表单中添加隐藏字段,例如
@Html.HiddenFor(m => m.Article.Id)