.net c#从部分视图重定向到带有json数据对象的视图

Redirect from partial view to view with json data object
2020-10-25
  •  译文(汉语)
  •  原文(英语)

我有一个json数据对象,我需要从局部视图传递到带有REDIRECT的视图.

可以说下面是我的局部视图(_createEmp.cshtml):-

Note:- This partial view is in different view or page

 <script type="text/javascript">

 $(document).ready(function () {
    LoadData();
 });

 function LoadData() {
  $.ajax({
     type: "GET",
     url: baseURL + "Employee/GetEmpInfo",
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     async: true,
     success: function (data) {
        console.log(data);
        **EmpData** = data; // EmpData object
     },
     error: function (error) {
        console.log("Error: " + error);
     }
     });
   }
 </script>

 <div>
     <input type="submit" value="Save" onclick="SetEmpInfo()" />
 </div>

我想将EmpData对象传输到另一个视图(让我们说NewEmp.cshtml),从传递的"EmpData对象"中绑定该视图中的某些内容,然后打开该视图(或使用REDIRECT来查看NewEmp.cshtml).

解决过程1

在使用ajax时,您可以从操作中返回URL并将其重定向到javascript中.

在没有看到控制器动作的情况下,您需要执行以下操作:

控制者

public ActionResult GetEmpInfo()
{
    // Do stuff
    return Json(new { success = true, redirecturl = Url.Action("GetEmpInfoSuccess") });
}

然后将类似的内容添加到javascript中的成功处理程序中:

Javascript(在成功处理程序中)

success: function (data) {
            if (data.success == true)
            {
               window.location = result.redirecturl;
             }
        }
解决过程2

发出请求以Employee/GetEmpInfo获取数据并成功重定向到其他视图-听起来不正确

我认为您可以一次访问服务器来完成所有这些操作:

  • 编写一个接收所有接收到的参数的动作GetEmpInfo.让我们称之为吧NewEmployee.
  • 如果GetEmpInfo动作是您的代码,请在动作内部重用它的逻辑NewEmployee来获取EmpData.如果不是您的代码,则可以使用问题异步请求HttpClient并获取EmpData- 所有这些都在服务器上执行
  • 拥有EmpData之后,您将拥有返回NewEmp视图所需的一切.

在这种特殊情况下,根本不需要AJAX.如果您需要发布一些数据或仅重定向到NewEmployee操作,则可以使用常规表单提交.

I have a json data object which i need to pass from partial view to view with REDIRECT.

Lets say below is my partial view ( _createEmp.cshtml ):-

Note:- This partial view is in different view or page

 <script type="text/javascript">

 $(document).ready(function () {
    LoadData();
 });

 function LoadData() {
  $.ajax({
     type: "GET",
     url: baseURL + "Employee/GetEmpInfo",
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     async: true,
     success: function (data) {
        console.log(data);
        **EmpData** = data; // EmpData object
     },
     error: function (error) {
        console.log("Error: " + error);
     }
     });
   }
 </script>

 <div>
     <input type="submit" value="Save" onclick="SetEmpInfo()" />
 </div>

And i want to transfer EmpData object to a different view (lets say NewEmp.cshtml), bind something in that view from passed "EmpData object" and open that view (or REDIRECT to view NewEmp.cshtml).

Solutions1

As you are using ajax, you could return the URL from your action and have it redirected in javascript.

Without seeing your controller action you would need to do something like this:

Controller

public ActionResult GetEmpInfo()
{
    // Do stuff
    return Json(new { success = true, redirecturl = Url.Action("GetEmpInfoSuccess") });
}

Then add something like this to your success handler in javascript:

Javascript (within your success handler)

success: function (data) {
            if (data.success == true)
            {
               window.location = result.redirecturl;
             }
        }
Solutions2

Issuing a request to Employee/GetEmpInfo for getting the data and on success redirecting to other view - doesn't sound right

I think that you can do all this with one trip to server:

  • Write an Action that receives all the the parameters that GetEmpInfo receives. Lets call it NewEmployee.
  • If GetEmpInfo action is your code, reuse it's logic inside NewEmployee action to get EmpData. If it is not your code, you can use issue async request with HttpClient and get EmpData - All this performed on the server
  • Once you have EmpData you should have everything your need to return a NewEmp view.

In this particular case there is no need in AJAX at all. You can use regular form submit in case that you need to post some data or just a redirect to NewEmployee action.

转载于:https://stackoverflow.com/questions/30400023/redirect-from-partial-view-to-view-with-json-data-object

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入