简体   繁体   English

将多个参数从Ajax Post传递到ASP.NET MVC控制器

[英]Pass multiple parameters from ajax post to asp.net mvc controller

I have a view that I'm binding with knockoutjs. 我认为我要与ockoutoutjs绑定。 I need to add data dynamically to a list in the viewmodel using ajax post. 我需要使用ajax发布将数据动态添加到viewmodel中的列表中。

            var data = {
                model: ko.toJS(self.Model),
                name: name
            }

             $.ajax({
                url: options.url + "AddAdvantage",
                type: 'POST',
                dataType: 'json',
                contentType: 'application/json',
                data: JSON.stringify(data),
                traditional: true,
                success: function (data) {
                    self.UpdateOnChange = false;
                    ko.mapping.fromJS(data, self.Model);
                    self.UpdateOnChange = true;
                }
            });

when the data parameter is passed to the controller action: 当data参数传递给控制器​​动作时:

[HttpPost]
public JsonResult AddAdvantage([ModelBinder(typeof(AdvantageModelBinder))] AdvantageViewModel model, string name) {
}

name value is passed, but model is always null 名称值已传递,但模型始终为null

I have tried this: 我已经试过了:

            var data = {
                model: ko.toJSON(self.Model),
                name: name
            }

also tried: 还尝试了:

            var data = JSON.stringify({
                model: ko.toJSON(self.Model),
                name: name
            });

same result. 同样的结果。

this works fine: 这工作正常:

           $.ajax({
                url: options.url + "AddAdvantage",
                type: 'POST',
                dataType: 'json',
                contentType: 'application/json',
                data: ko.toJSON(self.Model),
                traditional: true,
                success: function (data) {
                    self.UpdateOnChange = false;
                    ko.mapping.fromJS(data, self.Model);
                    self.UpdateOnChange = true;
                }
            });

My ModelBinder 我的ModelBinder

public class AdvantageModelBinder: DefaultModelBinder
{
    public AdvantageModelBinder()
    {
    }

    protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
    {
        ValueProviderResult value = bindingContext.ValueProvider.GetValue("WizardType");
        Type typeByTypeName = AdvantageModelBinder.GetTypeByTypeName((string)value.ConvertTo(typeof(string)));
        object obj = Activator.CreateInstance(typeByTypeName);
        bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => obj, typeByTypeName);
        return obj;
    }

    public static Type GetTypeByTypeName(string typeName)
    {
        Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
        for (int i = 0; i < (int)assemblies.Length; i++)
        {
            Type type = assemblies[i].GetType(typeName, false, true);
            if (type != null)
            {
                return type;
            }
        }
        throw new ArgumentException("Can't find the specified type in the loaded assemblies.", typeName);
    }
}

can anyone tell how to fix this? 谁能告诉我如何解决这个问题?

Seems that you're not passing exactly the same object in the payload. 似乎您没有在有效负载中传递完全相同的对象。

Let's say that your model object is like this: 假设您的模型对象是这样的:

{
   foo: 1,
   bar: 2
}

On the first two samples you are creating this object: 在前两个示例中,您将创建此对象:

{
   model: {
             foo: 1,
             bar: 2
          },
   name: "whatever"
}

So, when you pass it as an argument to the ajax call you're passing an object that is not expected on the MVC side. 因此,当您将其作为参数传递给ajax调用时,就传递了MVC端不期望的对象。

If you want to store it in a variable, you need to do the following: 如果要将其存储在变量中,则需要执行以下操作:

 var data = ko.toJSON(self.model)

And make the ajax call doing this: 并通过以下方式进行ajax调用:

 $.ajax({
            url: options.url + "AddAdvantage",
            type: 'POST',
            dataType: 'json',
            contentType: 'application/json',
            data: data,
            traditional: true,
            success: function (data) {
                self.UpdateOnChange = false;
                ko.mapping.fromJS(data, self.Model);
                self.UpdateOnChange = true;
            }
        });

Try this: 尝试这个:

var data = {
  model: self.Model,
  name: name
}

$.ajax({
  url: options.url + "AddAdvantage",
  type: 'POST',
  dataType: 'json',
  contentType: 'application/json',
  data: ko.toJSON(data),
  traditional: true,
  success: function (data) { ... }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM