0
@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。这是什么原因?

谢谢。

4

1 回答 1

2

MVC 在请求之间不会为您维护任何内容。当您发布到您的操作时,它只会发布您在表单中设置的值。由于表单中没有您的文章 ID 或用户 ID(或其他任何地方,例如路由或查询字符串),MVC 在您的 EditArticle 操作的模型绑定期间不会知道它们。

如果您希望通过您的帖子发送额外的详细信息,您可以在表单中放置隐藏字段,例如

@Html.HiddenFor(m => m.Article.Id)
于 2012-04-21T00:53:46.400 回答