一、问题描述
ajax在mvc中使用频繁,比如cms中的评论功能,但由于涉及到前后端开发,日久容易忘,在此做下记录。
二、内容
控制器中代码示例:
/// <summary> /// 在文章详细页面中提交评论 /// </summary> /// <param name="id">所查看文章Id</param> /// <returns></returns> public ActionResult AddArticleComment(FormCollection Form) { int Id =int.Parse(Form["Id"]); string EmailAddress = Form["EmailAddress"]; string CommentContent = Form["CommentContent"]; DateTime PublishTime = DateTime.Now; cmsAccess.addArticleCommentDb(Id,EmailAddress,CommentContent, PublishTime);//存入评论 return Content(""); }
cshtml代码示例:
<!--评论ajax提交--> <script type="text/javascript"> function pinglun() { $.ajax({ type: "Post", url: "/CMS/AddArticleComment", data: { Id: "@ViewBag.viewArticle.Id", EmailAddress: $("#txt1").val(), CommentContent: $("#txt2").val(), }, dataType: "json" }); }; </script> <!--/.评论ajax提交-->