【问题标题】:Post to MVC controller action to return a view using jquery发布到 MVC 控制器操作以使用 jquery 返回视图
【发布时间】:2016-06-07 10:36:53
【问题描述】:

我对微软的 MVC 框架有点陌生,似乎被困在这个问题上。

我正在尝试将一个 json 格式的 id 发布到我的控制器操作中,控制器返回一个带有模型的视图,所以我没有返回任何响应来处理数据的成功回调。该调用不需要是 ajax 调用,我想从我的表行上的点击事件中显示一个全新的视图。

我的代码如下:

Jquery,在我的表格主体行上添加了一个点击事件,当我点击一行时,我会获取该行的 id 并发布到特定的控制器操作(/Document/Details)

$(document).ready(function () {
$("#tbdocuments").on("click", "tbody tr", function (e) {
    var id = $(this).attr("data-id");
    console.log(id);
    $.post("/Document/Details", { "id": id })
});
})

Mvc 控制器:

控制器成功接收到客户端传来的id(点击表格行),我正确地获取我的模型传递给视图。

  public ActionResult Details(string id)
    {
        var model = documentHelper.FetchSingleDocument(Guid.Parse(id));

        return View(model);
    }

Razor 查看详情.cshtml:

这是我的标记所在的视图。

@model DocumentBuddy.Mvc.ViewModels.DocumentViewModel

@{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/_LayoutSecure.cshtml";
}
<h2>Details</h2>
<div>
<h4>DocumentViewModel</h4>
<hr />
<dl class="dl-horizontal">
    <dt>
        @Html.DisplayNameFor(model => model.DocumentId)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.DocumentId)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.Name)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.Name)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.Description)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.Description)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.MimeType)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.MimeType)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.Length)
    </dt>
    </dl>
</div>
<p>
 @Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
 @Html.ActionLink("Back to List", "Index")
</p>

单击表格行后,它会触发控制器并获取模型,但是“详细信息”视图永远不会显示,并且仍停留在同一视图上。我也检查了页面,没有显示任何错误。我很困惑如何在没有 ajax 调用的情况下在 MVC 中执行此操作,我需要重定向到新视图。任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 为什么不使用锚链接? href="/documents/details/3"。您是否希望他们能够单击表格行上的任意位置?
  • @mwwallace8,我希望整行都是可点击的,而且我正在通过 ajax 调用在 jquery 中构建表的主体,所以我创建了一个 tr 属性并将我的 json 数组附加到 tr然后附加到视图上的表格,我没有使用剃刀填充表格,所以那里没有href标签。

标签: jquery html post model-view-controller


【解决方案1】:

到目前为止,您的代码是完美的。您现在要做的就是显示详细信息页面。您必须在 post 方法中处理来自服务器的响应。它看起来像这样

$(document).ready(function () {
    $("#tbdocuments").on("click", "tbody tr", function (e) {
        var id = $(this).attr("data-id");
        console.log(id);
        $.post("/Document/Details", { "id": id }, function(detailsHtml){
           $("selector here").html(detailsHtml);
        })
    });
})

所以只需更新“此处的选择器”,它就会在所选元素中显示您的详细信息表单。

如果您想重定向到全新的详细信息页面,则不要使用 jquery post 方法,而是设置窗口位置。即

$(document).ready(function () {
    $("#tbdocuments").on("click", "tbody tr", function (e) {
        var id = $(this).attr("data-id");
        window.location.href = "/Document/Details/" + id;
    });
})

希望它清楚。

【讨论】:

  • jquery 代码(js 文件)位于文件夹控制器(控制器称为文件夹)的索引视图上。我想重定向到文档控制器中的详细信息视图。您根据我的理解添加的代码正在执行 ajax 调用,并将使用响应在文件夹控制器的索引视图中搜索和更新给定的选择器。我想在文档控制器中打开详细信息操作视图。 js 文件不会在详细信息视图中对 html 有任何引用。希望这是有道理的。
  • 我已经更新了答案。请看看是否有帮助。
  • @KiranJagathlal,如果您想重定向到详细信息视图,那么 不要 使用 ajax - 它毫无意义。 ajax 的全部意义在于留在同一页面上。
猜你喜欢
  • 1970-01-01
  • 2019-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多