記事の詳細のメイン ビューに記事モジュールのコメントを投稿する部分的なビューがあります。コメントのモデルには 3 つの必須フィールドID
(ID フィールド) と がArticleId
ありCommentText
ます。(Razor 構文を使用しています)
ActionArticleId
でコントローラーをパスしてみました。Create
public ActionResult Create(ArticleComment articlecomment, string AID)
{
articlecomment.ArticleId = AID; //this is required
if (User.Identity.IsAuthenticated)
{
articlecomment.UserId = WebSecurity.CurrentUserId.ToString();
}
else
{
articlecomment.UserId = Constants.Anonymus;
}
articlecomment.CommentDate = DateTime.Now;
if (ModelState.IsValid)
{
db.ArticleComment.Add(articlecomment);
int success = db.SaveChanges();
if (success > 0)
{
return Content("<script language='javascript' type='text/javascript'>alert('Comment added successfully.');window.location.href='" + articlecomment.ArticleId + "';</script>");
}
else
{
return Content("<script language='javascript' type='text/javascript'>alert('Posting comment has failed, please try later.');window.location.href='" + articlecomment.ArticleId+ "';</script>");
}
}
return PartialView(articlecomment);
}
しかし、まだModelState.IsValid
falseを返しています。次のコードを使用しましたModelState
がArticleId
、null になっていることがわかりました。
foreach (var modelStateValue in ViewData.ModelState.Values)
{
foreach (var error in modelStateValue.Errors)
{
// Do something useful with these properties
var errorMessage = error.ErrorMessage;
var exception = error.Exception;
}
}
ArticleId
隠しフィールドを使用するための値を設定することも考えましViewBag
たが、動作するコードは見つかりませんでした。私は次のことを試しました:
@Html.HiddenFor(model => model.ArticleId, new { @value = ViewBag.Article })
と
@Html.HiddenFor(model => model.ArticleId, (object)ViewBag.Article)
コメントを投稿する私の「ParticalView」は次のとおりです。
@model Outliner.Models.ArticleComment
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="editor-label">
@* @Html.HiddenFor(model => model.ArticleId, new { @value = ViewBag.Article })
@Html.HiddenFor(model => model.ArticleId, (object)ViewBag.Article)*@
@Html.LabelFor(model => model.Comment)
<span class="error">@Html.ValidationMessageFor(model => model.Comment)</span>
</div>
@Html.TextAreaFor(model => model.Comment)
<input type="submit" value="Post" />
}
これは、「ArticalDetail」ビュー (私のメイン ビュー) でこの部分ビューを呼び出す方法です。
@Html.Action("Create", "ArticleComment")
以前にビューのコントローラーで必要なフィールド値を渡しましたが、PartialView の問題に直面しています。私が間違っていることと、どうすればこれを機能させることができますか?
編集後、試してみてください
Satpal と Fals が私をある方向に導くので、私は彼らの提案を試し、次のことを試みました。
TryUpdateModel(articlecomment);
そしてまた
TryUpdateModel<ArticleComment>(articlecomment);
そしてまた
TryValidateModel(articlecomment);
しかし、ArticleId に対しても同じ検証エラーが発生していたので、Watch をチェックインしたところ、試したすべてのツリー メソッドがFalse
.
私も次のことを試しました:
UpdateModel(articlecomment);
と
UpdateModel<ArticleComment>(articlecomment);
上記のメソッドは例外を生成しています:
タイプ 'Outliner.Models.ArticleComment' のモデルを更新できませんでした。
これが私のモデルです:
[Table("ArticleComments")]
public class ArticleComment
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public string ArticleId { get; set; }
public string UserId { get; set; }
[Required]
[Display(Name = "Comment")]
public string Comment { get; set; }
[Required]
[Display(Name = "Commented On")]
public DateTime CommentDate { get; set; }
}
なぜ私のモデルが更新されないのか分かりません... :(