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
@{
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
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
None
0 Points
1 Post
json request from one view and send data to bind in another view in mvc4
Sep 17, 2020 06:29 AM|an_na|LINK
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);
}
});
})
All-Star
58124 Points
15635 Posts
Re: json request from one view and send data to bind in another view in mvc4
Sep 17, 2020 02:31 PM|bruce (sqlwork.com)|LINK
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
Contributor
2690 Points
771 Posts
Re: json request from one view and send data to bind in another view in mvc4
Sep 18, 2020 10:43 AM|YihuiSun|LINK
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
FileProcessAction
Here is the result.
Best Regards,
YihuiSun