locked
json request from one view and send data to bind in another view in mvc4 RRS feed

  • Question

  • User-1815195743 posted

    Controller:

       public ActionResult GetNewFileIdforMove(int FileNo  )
            {
                //RBACUser rUser = new RBACUser(Session["UserName"].ToString());
                //if (!rUser.HasPermission("Group_Edit"))
                //{
                //    return Json("D", JsonRequestBehavior.AllowGet);
                //}
                try
                {
                    FileMain FileMainAdd = new FileMain();
                    var data = _fileMainService.All().ToList().FirstOrDefault(x => x.FileNo == FileNo);
                    if (data != null)
                    {
                        FileMainAdd = data;
                    }                           
                    return Json(new { redirecturl = "http://localhost:/FileProcessAction/FileProcessAction", FileMainAdd = FileMainAdd }, JsonRequestBehavior.AllowGet);
                }
                catch (Exception)
                {
                    return Json("0", JsonRequestBehavior.AllowGet);
                }
            }

    View:

     $(document).on("click", "#add", function () {
                            var value = $(this).attr("value");
                            $.ajax({
                                url: '@Url.Action("GetNewFileIdforMove", "FileProcessAction")',
                                contentType: "application/json;charset=utf-8",
                                data: JSON.stringify({ FileNo: value }),
                                type: 'POST',
                                dataType: 'json',
                                success: function (datas) {                              
                                    window.location.href = datas.redirecturl;
                                    $("#FileID").val(datas.FileMainAdd.FileNo);
                                    $("#FileRef").val(datas.FileMainAdd.FileRef);
                                    $("#FileSub").val(datas.FileMainAdd.FileSub);
                                    $("#FileText").val(datas.FileMainAdd.FileText);
                                }
                            });
                        })

    Thursday, September 17, 2020 6:29 AM

All replies

  • User-474980206 posted

    I assume you want to pass the values to page loaded by the redirect. As the current page will be replaced, JavaScript can’t not access. You should pass the values as a query string on the redirect url. Or better yet just pass the fileno and do the lookup on the new page. Also not sure why the Ajax to just redirect instead of a standard form post. Pretty poor design

    Thursday, September 17, 2020 2:31 PM
  • User1686398519 posted

    Hi an_na, 

    According to your needs, you can store the data in the cookie and then retrieve it on the second page. I wrote an example, you can refer to it.

    Index

    @{
        ViewBag.Title = "Index";
        Layout = null;
    }
    <h2>Index</h2>
    <button id="add" value="123">add</button>
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script>
        $(document).on("click", "#add", function () {
            var value = $(this).attr("value");
            $.ajax({
                url: '@Url.Action("GetNewFileIdforMove", "FileProcessAction")',
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({ FileNo: value }),
                type: 'POST',
                dataType: 'json',
                success: function (datas) {
                    document.cookie = "FileID="+datas.FileMainAdd.FileNo;
                    document.cookie = "FileRef="+datas.FileMainAdd.FileRef;
                    document.cookie = "FileSub="+datas.FileMainAdd.FileSub;
                    document.cookie = "FileText="+datas.FileMainAdd.FileNo;
                    window.location.href = datas.redirecturl;
                }
            });
        });
    </script>

    FileProcessAction

    @{
        ViewBag.Title = "FileProcessAction";
        Layout = null;
    }
    <h2>FileProcessAction</h2>
    FileID:<input id="FileID" /><br />
    FileRef:<input id="FileRef" /><br />
    FileSub:<input id="FileSub" /><br />
    FileText:<input id="FileText" /><br />
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#FileID").val(getCookie("FileID"));
            $("#FileRef").val(getCookie("FileRef"));
            $("#FileSub").val(getCookie("FileSub"));
            $("#FileText").val(getCookie("FileText"));
            function getCookie(cname) {
                var name = cname + "=";
                var decodedCookie = decodeURIComponent(document.cookie);
                var ca = decodedCookie.split(';');
                for (var i = 0; i < ca.length; i++) {
                    var c = ca[i];
                    while (c.charAt(0) == ' ') {
                        c = c.substring(1);
                    }
                    if (c.indexOf(name) == 0) {
                        return c.substring(name.length, c.length);
                    }
                }
                return "";
            }
        });
    </script>

    Here is the result.

    Best Regards,

    YihuiSun

    Friday, September 18, 2020 10:43 AM