Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何返回到包含renderpartial的视图_Asp.net Mvc_Asp.net Ajax - Fatal编程技术网

Asp.net mvc 如何返回到包含renderpartial的视图

Asp.net mvc 如何返回到包含renderpartial的视图,asp.net-mvc,asp.net-ajax,Asp.net Mvc,Asp.net Ajax,我正在创建一个简单的博客,在我的文章中我有“评论”部分功能。我可以将数据传递给控制器并成功保存数据,但返回到包含“注释”的文章视图时会发生错误 以下是my Article.cshtml页面: 以下是错误: 参数字典包含“SimpleBlog.Controllers.HomeController”中方法“System.Web.Mvc.ActionResult ReadMoreInt32”的非null类型“System.Int32”的参数“Id”的null条目。可选参数必须是引用类型、可为null的

我正在创建一个简单的博客,在我的文章中我有“评论”部分功能。我可以将数据传递给控制器并成功保存数据,但返回到包含“注释”的文章视图时会发生错误

以下是my Article.cshtml页面:

以下是错误: 参数字典包含“SimpleBlog.Controllers.HomeController”中方法“System.Web.Mvc.ActionResult ReadMoreInt32”的非null类型“System.Int32”的参数“Id”的null条目。可选参数必须是引用类型、可为null的类型或声明为可选参数。 参数名称:参数


请帮助我,非常感谢。

您正在重定向到ReadMore的函数希望您传递int类型的id

请尝试以下操作:

[HttpPost]
        public ActionResult PostComment(Comment comment, int PostId)
        {
            if (comment != null)
            {
                comment.PostID = PostId;
                string sdate = DateTime.Now.ToShortDateString();
                comment.DateTime = DateTime.Parse(sdate);
                db.Comments.Add(comment);
                db.SaveChanges();
                return RedirectToAction("ReadMore",new { Id = PostId});
            }
            return View();
        }

非常感谢你。我做到了。
@model SimpleBlog.Comment

@using (Ajax.BeginForm("PostComment", "Home", new { PostId = Convert.ToInt32(ViewData["PostId"]) }, new AjaxOptions { HttpMethod = "POST" }))
{
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <div class="form-group">
        @Html.TextAreaFor(model => model.Body, 3, 10, new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.Body, "", new { @class = "text-danger" })
    </div>

    <input type="submit" value="Submit" class="btn btn-primary" />
}
[HttpPost]
        public ActionResult PostComment(Comment comment, int PostId)
        {
            if (comment != null)
            {
                comment.PostID = PostId;
                string sdate = DateTime.Now.ToShortDateString();
                comment.DateTime = DateTime.Parse(sdate);
                db.Comments.Add(comment);
                db.SaveChanges();
                return RedirectToAction("ReadMore");
            }
            return View();
        }
[HttpPost]
        public ActionResult PostComment(Comment comment, int PostId)
        {
            if (comment != null)
            {
                comment.PostID = PostId;
                string sdate = DateTime.Now.ToShortDateString();
                comment.DateTime = DateTime.Parse(sdate);
                db.Comments.Add(comment);
                db.SaveChanges();
                return RedirectToAction("ReadMore",new { Id = PostId});
            }
            return View();
        }