如何将 jquery.post 中的数据发送到使用 ViewModel 作为参数的 mvc 控制器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10803292/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:47:04  来源:igfitidea点击:

How to send data in jquery.post to mvc controller which use ViewModel as parameter?

jqueryasp.net-mvcvisual-studio-2010c#-4.0

提问by Radislav

I am writing application with asp.net mvc. I have controller with action, which use some ViewModel as parameter. How to send form data with jquery post to that mvc controller.

我正在用 asp.net mvc 编写应用程序。我有带动作的控制器,它使用一些 ViewModel 作为参数。如何使用 jquery post 将表单数据发送到该 mvc 控制器。

回答by Shyju

$.post("Yourcontroller/YourAction", { FirstName : $("#txtFirstName").val(), LastName : $("#txtLastName") } ,function(data){
  //do whatever with the response

});

Your ViewModel Property names and Parameter we are passing should be same. Ie : Your view model should have 2 properties called FirstNameand LastNamelike his

您的 ViewModel 属性名称和我们传递的参数应该相同。即:您的视图模型应该有 2 个属性被调用FirstName并且LastName喜欢他的

public class PersonViewModel
{
  public string FirstName { set;get;}
  public string LastName { set;get;}
  // other properties

}

And your Post action method should accept a parameter of type PersonViewModel

并且您的 Post 操作方法应该接受一个类型的参数 PersonViewModel

[HttpPost]
public ActionResult YourAction(PersonViewModel model)
{
  //Now check model.FirstName 
}

Alternatively, If your view is strongly typed to the PersonViewModel, you can simply send the serialized form to the action method using jQuery serializemethod

或者,如果您的视图是强类型到 PersonViewModel,您可以简单地使用 jQueryserialize方法将序列化的表单发送到操作方法

$.post("Yourcontroller/YourAction", $("#formId").serialize() ,function(data){
  //do whatever with the response

});

EDIT : As per the comment

编辑:根据评论

Serializewill take care of the Child property as well. Assume you have a class called Profession like this

Serialize也会照顾孩子的财产。假设您有一个名为 Profession 的类,如下所示

public class Profession
{
    public string ProfessionName { set; get; }
}

And your PersonViewModel has a property of type Profession

你的 PersonViewModel 有一个类型的属性 Profession

public class PersonViewModel
{
    //other properties
    public Profession Profession { set; get; }
    public PersonViewModel()
    {
        if (Profession == null)
            Profession = new Profession();
    }
}

You will get these data in your HttpPost Action method, if you fill that from your view.

如果您从视图中填充这些数据,您将在 HttpPost Action 方法中获得这些数据。

enter image description here

在此处输入图片说明

回答by Kamyar

var myData = {
              Parameter1: $("#someElementId").val(),
              Parameter2: $("#anotherElementId").val(),
              ListParameter: { /* Define IEnumerable collections as json array as well */}
              // more params here
             }  
$.ajax({
    url: 'someUrl',
    type: 'POST',
    dataType: "json",
    contentType: 'application/json',
    data: JSON.stringify(myData)
});  


[HttpPost]
public JsonResult Create(CustomViewModel vm)
{
    // You can access your ViewModel like a non-ajax call here.
    var passedValue = vm.Parameter1;
}

You can also serialize the whole form and pass it to your controller's action method. In you ajax call:

您还可以序列化整个表单并将其传递给控制器​​的操作方法。在你的ajax调用中:

data: $('form').serialize()