使用json数据对象从局部视图重定向到视图

时间:2015-05-22 14:59:53

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

我有一个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)。

2 个答案:

答案 0 :(得分:0)

当您使用ajax时,您可以从您的操作中返回该网址,并将其重定向到javascript中。

如果没有看到您的控制器操作,您需要执行以下操作:

<强>控制器

------------------------------------------------------------
C:\Python27\Scripts\pip run on 05/22/15 09:32:07
Downloading/unpacking pylzma
  Getting page https://pypi.python.org/simple/pylzma/
  URLs to search for versions for pylzma:
  * https://pypi.python.org/simple/pylzma/
  Analyzing links from page https://pypi.python.org/simple/pylzma/
    Skipping link https://pypi.python.org/packages/2.3/p/pylzma/pylzma-0.3.0-py2.3-win32.egg#md5=68b539bc322e44e5a087c79c25d82543 (from https://pypi.python.org/simple/pylzma/); unknown archive format: .egg
    Skipping link https://pypi.python.org/packages/2.3/p/pylzma/pylzma-0.3.0.win32-py2.3.exe#md5=cbbaf0541e32c8d1394eea89ce3910b7 (from https://pypi.python.org/simple/pylzma/); unknown archive format: .exe
    Skipping link https://pypi.python.org/packages/2.3/p/pylzma/pylzma-0.4.1-py2.3-win32.egg#md5=03829ce881b4627d6ded08c138cc8997 (from https://pypi.python.org/simple/pylzma/); unknown archive format: .egg
    Skipping link https://pypi.python.org/packages/2.3/p/pylzma/pylzma-0.4.2-py2.3-win32.egg#md5=1ae4940ad183f220e5102e32a7f5b496 (from https://pypi.python.org/simple/pylzma/); unknown archive format: .egg
    Skipping link https://pypi.python.org/packages/2.3/p/pylzma/pylzma-0.4.4-py2.3-win32.egg#md5=26849b5afede8a44117e6b6cb0e4fc4d (from https://pypi.python.org/simple/pylzma/); unknown archive format: .egg
    Skipping link https://pypi.python.org/packages/2.4/p/pylzma/pylzma-0.3.0-py2.4-win32.egg#md5=221208a0e4e9bcbffbb2c0ce80eafc11 (from https://pypi.python.org/simple/pylzma/); unknown archive format: .egg
    Skipping link https://pypi.python.org/packages/2.4/p/pylzma/pylzma-0.3.0.win32-py2.4.exe#md5=7152a76c28905ada5290c8e6c459d715 (from https://pypi.python.org/simple/pylzma/); unknown archive format: .exe
    Skipping link https://pypi.python.org/packages/2.4/p/pylzma/pylzma-0.4.1-py2.4-win32.egg#md5=c773b74772799b8cc021ea8e7249db46 (from https://pypi.python.org/simple/pylzma/); unknown archive format: .egg
    Skipping link https://pypi.python.org/packages/2.4/p/pylzma/pylzma-0.4.2-py2.4-win32.egg#md5=bf837af2374358f167008585c19d2f26 (from https://pypi.python.org/simple/pylzma/); unknown archive format: .egg
    Skipping link https://pypi.python.org/packages/2.4/p/pylzma/pylzma-0.4.4-py2.4-win32.egg#md5=9a657211e107da0261ed7a2f029566c4 (from https://pypi.python.org/simple/pylzma/); unknown archive format: .egg
    Skipping link https://pypi.python.org/packages/2.5/p/pylzma/pylzma-0.3.0-py2.5-win32.egg#md5=911d4e0b3cbf27c8e62abea1b6ded60e (from https://pypi.python.org/simple/pylzma/); unknown archive format: .egg
    Skipping link https://pypi.python.org/packages/2.5/p/pylzma/pylzma-0.3.0.win32-py2.5.exe#md5=bc1c3d4a402984056acf85a251ba347c (from https://pypi.python.org/simple/pylzma/); unknown archive format: .exe
    Skipping link https://pypi.python.org/packages/2.5/p/pylzma/pylzma-0.4.1-py2.5-win32.egg#md5=429f2087bf14390191faf6d85292186c (from https://pypi.python.org/simple/pylzma/); unknown archive format: .egg
    Skipping link https://pypi.python.org/packages/2.5/p/pylzma/pylzma-0.4.2-py2.5-win32.egg#md5=bf8036d15fd61d6a47bb1caf0df45e69 (from https://pypi.python.org/simple/pylzma/); unknown archive format: .egg
    Skipping link https://pypi.python.org/packages/2.5/p/pylzma/pylzma-0.4.4-py2.5-win32.egg#md5=3c8f6361bee16292fdbfda70f1dc0006 (from https://pypi.python.org/simple/pylzma/); unknown archive format: .egg
    Skipping link https://pypi.python.org/packages/2.6/p/pylzma/pylzma-0.4.1-py2.6-win32.egg#md5=4248c0e618532f137860b021e6915b32 (from https://pypi.python.org/simple/pylzma/); unknown archive format: .egg
    Skipping link https://pypi.python.org/packages/2.6/p/pylzma/pylzma-0.4.2-py2.6-win32.egg#md5=2c5f136a75b3c114a042f5f61bdd5d8a (from https://pypi.python.org/simple/pylzma/); unknown archive format: .egg
    Skipping link https://pypi.python.org/packages/2.6/p/pylzma/pylzma-0.4.4-py2.6-win32.egg#md5=8c7ae08bafbfcfd9ecbdffe9e4c9c6c5 (from https://pypi.python.org/simple/pylzma/); unknown archive format: .egg
    Skipping link https://pypi.python.org/packages/2.7/p/pylzma/pylzma-0.4.4-py2.7-win32.egg#md5=caee91027d5c005b012e2132e434f425 (from https://pypi.python.org/simple/pylzma/); unknown archive format: .egg
    Found link https://pypi.python.org/packages/source/p/pylzma/pylzma-0.3.0.tar.gz#md5=7ab1a1706cf3e19f2d10579d795babf7 (from https://pypi.python.org/simple/pylzma/), version: 0.3.0
    Found link https://pypi.python.org/packages/source/p/pylzma/pylzma-0.4.1.tar.gz#md5=b64557e8c4bcd0973f037bb4ddc413c6 (from https://pypi.python.org/simple/pylzma/), version: 0.4.1
    Found link https://pypi.python.org/packages/source/p/pylzma/pylzma-0.4.2.tar.gz#md5=ab37d6ce2374f4308447bff963ae25ef (from https://pypi.python.org/simple/pylzma/), version: 0.4.2
    Found link https://pypi.python.org/packages/source/p/pylzma/pylzma-0.4.3.tar.gz#md5=e53d40599ca2b039dedade6069724b7b (from https://pypi.python.org/simple/pylzma/), version: 0.4.3
    Found link https://pypi.python.org/packages/source/p/pylzma/pylzma-0.4.4.tar.gz#md5=a2be89cb2288174ebb18bec68fa559fb (from https://pypi.python.org/simple/pylzma/), version: 0.4.4
    Found link https://pypi.python.org/packages/source/p/pylzma/pylzma-0.4.5.tar.gz#md5=4fda4666c60faa9a092524fdda0e2f98 (from https://pypi.python.org/simple/pylzma/), version: 0.4.5
    Found link https://pypi.python.org/packages/source/p/pylzma/pylzma-0.4.6.tar.gz#md5=140038c8c187770eecfe7041b34ec9b9 (from https://pypi.python.org/simple/pylzma/), version: 0.4.6
  Using version 0.4.6 (newest of versions: 0.4.6, 0.4.5, 0.4.4, 0.4.3, 0.4.2, 0.4.1, 0.3.0)
  Downloading from URL https://pypi.python.org/packages/source/p/pylzma/pylzma-0.4.6.tar.gz#md5=140038c8c187770eecfe7041b34ec9b9 (from https://pypi.python.org/simple/pylzma/)
  Running setup.py (path:c:\users\username\appdata\local\temp\pip_build_username\pylzma\setup.py) egg_info for package pylzma
    running egg_info
    creating pip-egg-info\pylzma.egg-info
    writing requirements to pip-egg-info\pylzma.egg-info\requires.txt
    writing pip-egg-info\pylzma.egg-info\PKG-INFO
    writing top-level names to pip-egg-info\pylzma.egg-info\top_level.txt
    writing dependency_links to pip-egg-info\pylzma.egg-info\dependency_links.txt
    writing manifest file 'pip-egg-info\pylzma.egg-info\SOURCES.txt'
    warning: manifest_maker: standard file '-c' not found

    reading manifest file 'pip-egg-info\pylzma.egg-info\SOURCES.txt'
    reading manifest template 'MANIFEST.in'
    warning: no files found matching '*.py' under directory 'test'
    warning: no files found matching '*.7z' under directory 'test'
    no previously-included directories found matching 'src\sdk.orig'
    writing manifest file 'pip-egg-info\pylzma.egg-info\SOURCES.txt'
  Source in c:\users\username\appdata\local\temp\pip_build_username\pylzma has version 0.4.6, which satisfies requirement pylzma
Installing collected packages: pylzma
  Running setup.py install for pylzma
    Running command C:\Python27\python.exe -c "import setuptools, tokenize;__file__='c:\\users\\username\\appdata\\local\\temp\\pip_build_username\\pylzma\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record c:\users\username\appdata\local\temp\pip-4onyx_-record\install-record.txt --single-version-externally-managed --compile
    running install
    running build
    running build_py
    creating build
    creating build\lib.win32-2.7
    copying py7zlib.py -> build\lib.win32-2.7
    running build_ext
    adding support for multithreaded compression
    building 'pylzma' extension
    creating build\temp.win32-2.7
    creating build\temp.win32-2.7\Release
    creating build\temp.win32-2.7\Release\src
    creating build\temp.win32-2.7\Release\src\pylzma
    creating build\temp.win32-2.7\Release\src\sdk
    creating build\temp.win32-2.7\Release\src\7zip
    creating build\temp.win32-2.7\Release\src\7zip\C
    creating build\temp.win32-2.7\Release\src\compat
    C:\Users\username\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -DWITH_COMPAT=1 -DPYLZMA_VERSION="0.4.6" -DCOMPRESS_MF_MT=1 -Isrc/sdk -IC:\Python27\include -IC:\Python27\PC /Tcsrc/pylzma/pylzma.c /Fobuild\temp.win32-2.7\Release\src/pylzma/pylzma.obj /MT
    cl : Command line warning D9025 : overriding '/MD' with '/MT'
    pylzma.c
    src/pylzma/pylzma.c(284) : error C2440: 'function' : cannot convert from 'double' to 'const char *'
    src/pylzma/pylzma.c(284) : warning C4024: 'PyModule_AddStringConstant' : different types for formal and actual parameter 3
    src/pylzma/pylzma.c(284) : error C2143: syntax error : missing ')' before 'constant'
    src/pylzma/pylzma.c(284) : error C2059: syntax error : ')'
    error: command 'C:\\Users\\username\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\cl.exe' failed with exit status 2
    Complete output from command C:\Python27\python.exe -c "import setuptools, tokenize;__file__='c:\\users\\username\\appdata\\local\\temp\\pip_build_username\\pylzma\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record c:\users\username\appdata\local\temp\pip-4onyx_-record\install-record.txt --single-version-externally-managed --compile:
    running install

running build

running build_py

creating build

creating build\lib.win32-2.7

copying py7zlib.py -> build\lib.win32-2.7

running build_ext

adding support for multithreaded compression

building 'pylzma' extension

creating build\temp.win32-2.7

creating build\temp.win32-2.7\Release

creating build\temp.win32-2.7\Release\src

creating build\temp.win32-2.7\Release\src\pylzma

creating build\temp.win32-2.7\Release\src\sdk

creating build\temp.win32-2.7\Release\src\7zip

creating build\temp.win32-2.7\Release\src\7zip\C

creating build\temp.win32-2.7\Release\src\compat

C:\Users\username\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -DWITH_COMPAT=1 -DPYLZMA_VERSION="0.4.6" -DCOMPRESS_MF_MT=1 -Isrc/sdk -IC:\Python27\include -IC:\Python27\PC /Tcsrc/pylzma/pylzma.c /Fobuild\temp.win32-2.7\Release\src/pylzma/pylzma.obj /MT

cl : Command line warning D9025 : overriding '/MD' with '/MT'

pylzma.c

src/pylzma/pylzma.c(284) : error C2440: 'function' : cannot convert from 'double' to 'const char *'

src/pylzma/pylzma.c(284) : warning C4024: 'PyModule_AddStringConstant' : different types for formal and actual parameter 3

src/pylzma/pylzma.c(284) : error C2143: syntax error : missing ')' before 'constant'

src/pylzma/pylzma.c(284) : error C2059: syntax error : ')'

error: command 'C:\\Users\\username\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\cl.exe' failed with exit status 2

----------------------------------------
Cleaning up...
  Removing temporary dir c:\users\username\appdata\local\temp\pip_build_username...
Command C:\Python27\python.exe -c "import setuptools, tokenize;__file__='c:\\users\\username\\appdata\\local\\temp\\pip_build_username\\pylzma\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record c:\users\username\appdata\local\temp\pip-4onyx_-record\install-record.txt --single-version-externally-managed --compile failed with error code 1 in c:\users\username\appdata\local\temp\pip_build_username\pylzma
Exception information:
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\pip\basecommand.py", line 122, in main
    status = self.run(options, args)
  File "C:\Python27\lib\site-packages\pip\commands\install.py", line 283, in run
    requirement_set.install(install_options, global_options, root=options.root_path)
  File "C:\Python27\lib\site-packages\pip\req.py", line 1435, in install
    requirement.install(install_options, global_options, *args, **kwargs)
  File "C:\Python27\lib\site-packages\pip\req.py", line 706, in install
    cwd=self.source_dir, filter_stdout=self._filter_install, show_stdout=False)
  File "C:\Python27\lib\site-packages\pip\util.py", line 697, in call_subprocess
    % (command_desc, proc.returncode, cwd))
InstallationError: Command C:\Python27\python.exe -c "import setuptools, tokenize;__file__='c:\\users\\username\\appdata\\local\\temp\\pip_build_username\\pylzma\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record c:\users\username\appdata\local\temp\pip-4onyx_-record\install-record.txt --single-version-externally-managed --compile failed with error code 1 in c:\users\username\appdata\local\temp\pip_build_username\pylzma

然后在javascript中将这样的内容添加到您的成功处理程序中:

Javascript (在您的成功处理程序中)

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

答案 1 :(得分:0)

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

我认为你可以通过一次服务器来完成所有这些:

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

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