从视图到控制器的数据,以查看

时间:2017-04-28 17:13:52

标签: c# asp.net ajax asp.net-mvc

我正在尝试将隐藏文本框的数据从索引获取到另一个视图中的文本字段。

首先,我使用AJAX获取数据。

//ajax
             if (id && toolcode && shopdoccode) {
                 $.ajax({
                     type: 'GET',
                     url: '@Url.Action("delivery", "service")',
            data: { id, memo },
            success: function (data) {
                if (data.success) {
                    console.log("Succes");
                    window.location.href = '@Url.Action("delivery", "service")';
                }

            },
            error: function (data) {
                console.log("Error");
            }
        });
             }
             window.location.href = ("service/delivery?id=" + id + "&toolcode=" + toolcode + "&regdate=" + regdate + "&shopdoccode=" + shopdoccode + "&memo" + memo);
}

然后,我在控制器中创建一个viewbag,将数据传递给我的另一个视图。

public ActionResult Delivery(string id, string memo)
        {
            ServiceOrder service = GetService(id);

            ViewBag.id = id;
            ViewBag.memo = memo;

最后,在视图中

  @using (Html.BeginForm("GeneratePDF", "Service", FormMethod.Post, new { @class = "form-style-4", id = "getPDFForm" }))
    {
        string memofield = ViewBag.memo;
        //code
       <textarea name="jobdescription" readonly>@memofield</textarea>

如果我在控制器中设置断点,我可以看到Viewbag.memo包含正确的数据。进入视图后,我在字符串memofield = viewbag.memo上设置了一个断点 即使在那里,我也能看到数据。但是,突然之间,如果我按下继续,字符串将返回'null'

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

可能会发生这种情况,因为在您发送ajax请求后,您将重定向回传递操作window.location,其中不包含帖子参数ID和备忘录..请参阅

    success: function (data) {
      if (data.success) {
          console.log("Succes");
          window.location.href = '@Url.Action("delivery", "service")';
      }
   }
相关问题