本文介绍了如何从视图传递数据用ajax获取或交的MVC与参数控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从View数据传递给控制器​​的操作方法用ajax如下: -

我有我通过从其他控制器添加到该视图下方使用viewbag有点像用户的成员资格实例此ViewBag.MyUser = MYUSER;

现在我想用下面的ajax通过MYUSER到另一个控制器形式的这一观点。

  $('#链接')。点击(函数(){
        $阿贾克斯({
            网址:HTTP://本地主机/客户/过程,
            输入:POST,
            数据:@ ViewBag.MyUser',
            成功:函数(){
            },
            错误:函数(){
            }
        });

该ActionMethod到我张贴如下:

 公众的ActionResult过程(MYUSER的MembershipUser)
{
   //做somethihng与MYUSER
}

如果我通过做阿贾克斯后,我得到的错误在内部 BeginExecuteCore(AsyncCallback的回调,对象的状态),指出没有参数的构造函数此对象定义。和控制甚至不来我actionmethod。

如果我删除了参数(MYUSER的MembershipUser)从操作方法将其发布到操作方法,但随后


  1. 如何传递在这种情况下MYUSER没有从该视图参数控制器?

  2. 是有什么错路线?如果是的话应该是什么路线?

  3. 或者我应该使用GET或POST?

  4. 我应该在哪里投了MYUSER回的MembershipUser?


解决方案

问题是你无法通过的 MYUSER 的从JQuery的参数,因为jQuery不知道班级的的MembershipUser 的。请记住,jQuery是一个客户端的语言和的MembershipUser 的在C#中的定义是在服务器端。

您可以通过您从 MYUSER 的需要的属性对象的处理的行动使用的 GET 的如下步骤(supossing的 MYUSER 的对象和 ID 的一个一个的名称的):

  $('#链接')。点击(函数(){
    $阿贾克斯({
        网址:HTTP://本地主机/客户/过程,
        输入:GET,
        数据:{
                ID:@ ViewBag.MyUser.ID
                名称:@ ViewBag.MyUser.Name
              },
        成功:函数(){
        },
        错误:函数(){
        }
    });

动作应该是这样的:

 公众的ActionResult过程(INT ID,字符串名称)
{
   //做一点事
}

我希望它可以帮助你!

I am trying to pass data from View to Controller Action Method using ajax as follows:-

I have Membership instance of user which I passed in from another controller to this view below using viewbag somewhat like this ViewBag.MyUser = MyUser;

Now I want to pass 'MyUser' to another Controller form this view using ajax as below.

 $('#Link').click(function () {
        $.ajax({
            url: http://localhost/Account/Process,
            type: 'POST',
            data: '@ViewBag.MyUser',
            success: function () {
            },
            error: function () {
            }
        });

The ActionMethod to which I am posting is as follows

public ActionResult Process(MembershipUser MyUser)
{
   //Do somethihng with MyUser
}

If I pass do ajax post, I get error internally at BeginExecuteCore(AsyncCallback callback, object state) stating that 'No parameterless constructor defined for this object.' and control does not even comes to my actionmethod.

If I remove the parameter (MembershipUser MyUser) from Action Method it posts to Action method, but then

  1. how can i pass 'MyUser' in such case without parameter from that view to controller ?
  2. is there something wrong with routes ? if yes what should be the route ?
  3. or should i use get or post ?
  4. Where should i cast the MyUser back to MembershipUser ?
解决方案

The problem is you can't pass MyUser as parameter from JQuery because JQuery doesn't know the class MembershipUser. Remember that JQuery is a client side language and MembershipUser is defined in C# on the server side.

You could pass the properties that you need from the MyUser object to the Process action using GET as follows (supossing that the MyUser object has and ID an a Name):

$('#Link').click(function () {
    $.ajax({
        url: http://localhost/Account/Process,
        type: 'GET',
        data: {
                id: "@ViewBag.MyUser.ID",
                name: "@ViewBag.MyUser.Name"
              },
        success: function () {
        },
        error: function () {
        }
    });

The action should be something like this:

public ActionResult Process(int id, string name)
{
   //Do something
}

I hope it helps you!

这篇关于如何从视图传递数据用ajax获取或交的MVC与参数控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 07:19