如何通过MVC中的部分视图插入数据

时间:2018-01-10 13:48:59

标签: c# asp.net-mvc partial-views

我想使用部分视图为我在下面提到的这个动作方法插入数据。

问题是,当我提交表格时,它没有给我任何回应。

如果发布评论,我可以用哪个帖子发布评论并重定向到此页面?

我的部分视图位于共享文件夹中。

    [HttpPost]
    [ValidateAntiForgeryToken]
    public JsonResult Comments([Bind(Include = "CommentId,UserComments,UserId")] Comment comment)
    {
        if (ModelState.IsValid)
        {
            db.Comments.Add(comment);
            db.SaveChanges();
            return Json(comment, JsonRequestBehavior.AllowGet);
        }
        ViewBag.UserId = new SelectList(db.UserAccounts, "UserId", "FirstName", comment.UserId);
        return Json(JsonRequestBehavior.AllowGet);
    }

在布局中渲染局部视图。

 @Html.Partial("_Comments", new OnlineTaxiReservationSystem.Models.Comment())

,局部视图在这里:

  @model OnlineTaxiReservationSystem.Models.Comment

 <script type="text/javascript">
$(document).on('click', '.btnSubmit', function () {
    $.ajax({
        url: '@Url.Action("Comments", "Home")',
        cache: false,
        async: true,
        data: {
            //put the data that you want to save from the partial here
            UserId: $('#userId').val(),
            UserComments: $('#content').val()
        },
        success: function (_result) {
            window.location.href = "Home/Index";
        }
    });
});

  @if (Session["user"] != null)
     {
   using (Html.BeginForm())
   {
      @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Your Feedback</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group" style="margin-left: 9%">
            @Html.LabelFor(model => model.UserId, "User Name", htmlAttributes: new { @class = "control-label col-md-2" })
            @*@Html.Label(Session["UserName"].ToString(), htmlAttributes: new { @class = "control-label label-primary col-md-1" })*@
            <div class="col-md-4">
                @Html.Editor("UserId", null, new { htmlAttributes = new { @class = "form-control", @id = "userId" } })
                @Html.ValidationMessageFor(model => model.UserId, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.UserComments, htmlAttributes: new { @class = "control-label col-md-1" })
            <div class="col-lg-12">
                @Html.TextAreaFor(model => model.UserComments, new { htmlAttributes = new { @class = "form-control", @id = "content"} })
                @Html.ValidationMessageFor(model => model.UserComments, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-1 col-md-10">
                <input type="submit" value="Submit" id="btnSubmit" class="btn btn-primary" />
            </div>
        </div>
    </div>
  }

  <div>
    @Html.ActionLink("Back to List", "Index")
  </div>
  }

1 个答案:

答案 0 :(得分:1)

我认为您正在拨打错误的网址:

如果您不想通过按钮提交,而是通过javascript提交,则需要添加e.preventDefault();

$(document).on('click', '.btnSubmit', function () {
    e.preventDefault(); //cancel the default behavior
    //do this instead (the ajax call):
    $.ajax({
        url: '@Url.Action("Comments", "Home")',
        cache: false,
        async: true,
        data: {
            //put the data that you want to save from the partial here
            UserId: $('#userId').val(),
            UserComments: $('#content').val()
        },
        success: function (_result) {
            window.location.href = "Home/Index";
        }
    });
});

如果您能提供404电话的全部详细信息,我可能会更加精彩。