简体   繁体   English

将int数组传递给MVC Controller

[英]Passing an int array to MVC Controller

I'm trying to pass an array of int from JavaScript to an MVC controller that accepts 2 parameters - an int array and an int. 我正在尝试将一个int数组从JavaScript传递给一个接受2个参数的MVC控制器 - 一个int数组和一个int。 This is to perform a Page Redirect to the View returned by the Controller Action. 这是执行页面重定向到Controller Action返回的视图。

var dataArray = getAllIds(); //passes back a JavaScript array 
window.location.replace("/" + controllerName + "/EditAll?ids=" + dataArray + "&currentID=" + dataArray[0])

dataArray contains 1,7 as expected for my sample usage. dataArray包含1,7个我的样本用法。

Controller Code 控制器代码

public virtual ActionResult EditAll(int[] ids, int currentID)
{

  currentModel = GetID(currentID);
  currentVM = Activator.CreateInstance<ViewModel>();
  currentVM.DB = DB;
  currentVM.Model = currentModel;
  currentVM.ViewMode = ViewMode.EditAll;
  currentVM.ModelIDs = ids;

  if (currentModel == null)
  {
      return HttpNotFound();
  }

  return View("Edit", MasterName, currentVM);
}

The issue is that when inspecting the int[] ids passed to the controller, it's value is null. 问题是当检查传递给控制器​​的int [] id时,它的值为null。 currentID is set to 1 as expected. currentID按预期设置为1。

I've tried setting jQuery.ajaxSettings.traditional = true which has no effect I've also tried creating a server side url using @Url.Action in JavaScript. 我已经尝试设置jQuery.ajaxSettings.traditional = true这没有效果我也尝试在JavaScript中使用@ Url.Action创建服务器端URL。 I also tried JSON.Stringify before passing the array eg 在传递数组之前我也尝试过JSON.Stringify

window.location.replace("/" + controllerName + "/EditAll?ids=" + JSON.stringify(dataArray) + "&currentID=" + dataArray[0])

Again the id array ends up null on the controller side. 同样,id数组在控制器端最终为null。

Does anyone have any pointers on getting the int array to pass to the controller correctly? 有没有人有任何关于让int数组正确传递给控制器​​的指针? I could declare the parameter as String in the Controller Action and manually serialize and de-serialize the parameter but I need to understand how to get the framework to automatically do simple type conversions. 我可以在Controller Action中将参数声明为String并手动序列化和反序列化参数,但我需要了解如何让框架自动执行简单的类型转换。

Thanks! 谢谢!

To pass an array of simple values in MVC, you simply need to give the same name to multiple values eg The URI will end up looking something like this 要在MVC中传递一组简单值,您只需要为多个值赋予相同的名称,例如,URI最终会看起来像这样

/{controllerName}/EditAll?ids=1&ids=2&ids=3&ids=4&ids=5&currentId=1

The default model binding in MVC will bind this to the int array Action parameter correctly. MVC中的默认模型绑定将正确地将其绑定到int数组Action参数。

Now, if it were an array of complex values, there are two approaches that can be taken for model binding. 现在,如果它是一个复杂值的数组,则可以采用两种方法进行模型绑定。 Let's suppose you have a type like so 我们假设您有类似的类型

public class ComplexModel
{
    public string Key { get; set; }

    public string Value { get; set; }
}

and a controller action signature of 和控制器动作签名

public virtual ActionResult EditAll(IEnumerable<ComplexModel> models)
{
}

For model binding correctly, values need to include an indexer in the request eg 对于正确的模型绑定,值需要在请求中包含索引器,例如

/{controllerName}/EditAll?models[0].Key=key1&models[0].Value=value1&models[1].Key=key2&models[1].Value=value2

We're using an int indexer here but you can imagine that this might be quite inflexible in an application where items presented to a user in the UI can be added and deleted at any index/slot in the collection. 我们在这里使用的是一个int索引器,但你可以想象这在一个应用程序中可能非常不灵活,在这个应用程序中,可以在集合中的任何索引/插槽中添加和删除在UI中呈现给用户的项目。 For this purpose, MVC also allows you to specify your own indexer for each item in the collection and pass that value in with the request for the default model binding to use eg 为此,MVC还允许您为集合中的每个项目指定自己的索引器,并将该值传递给默认模型绑定的请求以使用,例如

/{controllerName}/EditAll?models.Index=myOwnIndex&models[myOwnIndex].Key=key1&models[myOwnIndex].Value=value1&models.Index=anotherIndex&models[anotherIndex].Key=key2&models[anotherIndex].Value=value2

Here we have specified our own indexers, myOwnIndex and anotherIndex for model binding to use to bind a collection of complex types. 在这里,我们为模型绑定指定了自己的索引器myOwnIndexanotherIndex用于绑定复杂类型的集合。 You can use any string for your indexers as far as I am aware. 据我所知,您可以为索引器使用任何字符串。

Alternatively, you can implement your own model binder to dictate how an incoming request should be bound to a model. 或者,您可以实现自己的模型绑定器来指示传入请求应如何绑定到模型。 This requires more work than using the default framework conventions but does add another layer of flexibility. 这需要比使用默认框架约定更多的工作,但确实增加了另一层灵活性。

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

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