Webページにコメントを追加するコントローラを作成しました。以下の私のコメントコントローラフォームを送信しないエラーは、mvcのパラメータのヌルエントリになります。4
public ActionResult AddComment(int id=0)
{
int spid = id;
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddComment(comment cmt,int spid)
{
if (ModelState.IsValid)
{
cmt.SPID = spid;
db.comments.Add(cmt);
db.SaveChanges();
return RedirectToAction("Index", "Comment");
}
return View(cmt);
}
はこれがでaddCommentビュー以下
@model WEB1.Models.comment
@using (Html.BeginForm("AddComment", "Comment"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>comment</legend>
<div class="editor-label">
@Html.LabelFor(model => model.cmd_content)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.cmd_content)
@Html.ValidationMessageFor(model => model.cmd_content)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.t_email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.t_email)
@Html.ValidationMessageFor(model => model.t_email)
</div>
@Html.HiddenFor(model => model.SPID)
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
は、私は、エラーメッセージ
「辞書が含まれているパラメータを持っているコメントモデル
[Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int CMT_ID { get; set; } private DateTime _date = DateTime.Now; public DateTime cmd_ad { get { return _date; } set { _date = value; } } [Required(ErrorMessage = "Please add Comment before submit")] public string cmd_content { get; set; } [RegularExpression(@"^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$", ErrorMessage = "Please Enter Correct Email Address")] public string t_email { get; set; } public Nullable<int> SPID { get; set; }
ですされますnon-nuのパラメータ 'spid'に対するヌルエントリ'WEB1.Controllers.CommentController'内のメソッド 'System.Web.Mvc.ActionResult AddComment(WEB1.Models.comment、Int32)'に対して 'llable'タイプの 'System.Int32'を返します。オプションのパラメータは、参照型、null可能型、またはオプションのパラメータとして宣言する必要があります。 パラメータ名:parameters '。
ポストアクションメソッドにヒットしないコードをデバッグしました。この問題を解決する方法
の
SPID
プロパティに私は私の質問 – OK91@StephenMueckeのおかげlot.Itの作品を修正しました – OK91