@Html.ActionLink("Edit", "EditArticle", new { ArticleID = article.ArticleID })
I retrieved Article by ArticleID and return to edit page like this:
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);
}
There is no problem until here.
And in my editing page I’m changing some attributes of article (not all attributes).For example I’m changing title, content, and updateddate. Like this:
@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>
}
And then I post it to:
[HttpPost]
public ActionResult EditArticle(AddArticleModel AddArticleModel, String CategoryID)
{
//TODO: update database...
return View(AddArticleModel);
}
But unchanged attributes are return null(ArticleID, UserID, etc).So I cant Update the database, Because I dont have ArticleID after posting. What is the reason for this?
Thanks.
MVC doesn’t maintain anything for you between requests. When you post to your action, it will post only the values that you have set up in your form. As you don’t have your article id or user id in the form (or anywhere else, e.g. route or query string), MVC won’t know about them during model binding for your EditArticle action.
If you want the extra details to be sent through with your post, you can put hidden fields in the form, e.g.