jQuery 如何使用 ajax get 或 post 在 mvc 中使用参数将数据从视图传递到控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15931641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to pass data from View to Controller using ajax get or post in mvc with parameters
提问by user2232861
I am trying to pass data from View to Controller Action Method using ajax as follows:-
我正在尝试使用 ajax 将数据从视图传递到控制器操作方法,如下所示:-
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;
我有用户的 Membership 实例,我使用 viewbag 从另一个控制器传递到下面的这个视图,有点像这个 ViewBag.MyUser = MyUser;
Now I want to pass 'MyUser' to another Controller form this view using ajax as below.
现在我想使用 ajax 将“MyUser”传递给另一个控制器形成这个视图,如下所示。
$('#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
我发布的 ActionMethod 如下
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.
如果我通过 do ajax post,我会在内部收到错误, BeginExecuteCore(AsyncCallback callback, object state)
指出“没有为此对象定义无参数构造函数”。我的行动方法甚至没有控制权。
If I remove the parameter (MembershipUser MyUser)
from Action Method it posts to Action method, but then
如果我(MembershipUser MyUser)
从 Action Method 中删除参数,它会发布到 Action 方法,但是
- how can i pass 'MyUser' in such case without parameter from that view to controller ?
- is there something wrong with routes ? if yes what should be the route ?
- or should i use get or post ?
- Where should i cast the MyUser back to MembershipUser ?
- 在这种情况下,我如何在没有参数的情况下将“MyUser”从该视图传递给控制器?
- 路线有问题吗?如果是的话应该是什么路线?
- 或者我应该使用 get 还是 post ?
- 我应该在哪里将 MyUser 转换回 MembershipUser ?
回答by danieleduardo29
The problem is you can't pass MyUseras parameter from JQuery because JQuery doesn't know the class MembershipUser. Remember that JQuery is a client side language and MembershipUseris defined in C# on the server side.
问题是您不能将MyUser作为参数从 JQuery传递,因为 JQuery 不知道类MembershipUser。请记住,JQuery 是一种客户端语言,而MembershipUser是在服务器端用 C# 定义的。
You could pass the properties that you need from the MyUserobject to the Processaction using GETas follows (supossing that the MyUserobject has and IDan a Name):
您可以使用GET将您需要的属性从MyUser对象传递给Process操作,如下所示(假设MyUser对象具有和ID一个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!
我希望它能帮助你!