传递一个int数组MVC控制器 [英] Passing an int array to MVC Controller

查看:198
本文介绍了传递一个int数组MVC控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从JavaScript一个int数组传递给MVC控制器接受2个参数 - 一个int数组,一个int。这是执行页转到由控制器操作返回的视图。

  VAR dataArray中= getAllIds(); //传回一个JavaScript数组
window.location.replace(/+ controllerName +?/ EditAll IDS =+ dataArray中+与& currentID =+ dataArray中[0])

dataArray中含有1,7预期为我的样品使用。

控制器code

 公共虚拟的ActionResult EditAll(INT [] IDS,INT currentID)
{  currentModel =的getId(currentID);
  currentVM = Activator.CreateInstance<视图模型>();
  currentVM.DB = DB;
  currentVM.Model = currentModel;
  currentVM.V​​iewMode = ViewMode.EditAll;
  currentVM.ModelIDs = IDS;  如果(currentModel == NULL)
  {
      返回HttpNotFound();
  }  返回视图(编辑,MasterName,currentVM);
}

问题是,检查传递给控制器​​中的int [] ID时,它的值为null。 currentID如预期设定为1

我试过设置jQuery.ajaxSettings.traditional =真有没有效果
我也试着创建使用@ Url.Action在JavaScript中的服务器端的URL。
我也试过J​​SON.Stringify传递数组例如之前。

  window.location.replace(/+ controllerName +?/ EditAll IDS =+ JSON.stringify(dataArray中)+&放大器; currentID =+ dataArray中[0] )

同样的ID阵列最终空控制器侧。

有没有人有获得int数组正确地传递给控制器​​的任何指针?我可以声明参数为String的控制器动作和手动序列化和反序列化的参数,但我需要了解如何让框架自动做简单的类型转换。

谢谢!


解决方案

要通过简单的值的数组中的MVC,你只需把同一个名字给多个值,例如该URI最终会看起来像这样

  / {controllerName} / EditAll IDS = 1&安培; IDS = 2及IDS = 3及IDS = 4和IDS = 5和currentId = 1

默认的模型MVC绑定将结合这对正确int数组操作参数。

现在,如果它是复杂值的阵列,有可采取模型绑定两种方法。让我们假设你有一个类型的,像这样

 公共类ComplexModel
{
    公共字符串键{获得;组; }    公共字符串值{获得;组; }
}

和一个控制器动作签名

 公共虚拟的ActionResult EditAll(IEnumerable的< ComplexModel>型号)
{
}

有关模型正确绑定,值需要在请求中包含例如一个索引。

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

我们在这里使用了一个 INT 索引器,但你可以想像,这可能会在项目presented到用户在UI的应用程序是相当不灵活可添加和删​​除在集合中的任何索引/插槽。为此,MVC还允许您指定自己的索引集合中的每个项目,并与默认模型绑定使用例如请求传递价值。

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

在这里,我们已经指定我们自己的索引, myOwnIndex anotherIndex 为模型绑定使用绑定的集合复杂类型。您可以使用您的索引任何字符串据我所知。

另外,您也可以实现自己的模型绑定口述传入的请求如何绑定到一个模型。这需要比使用默认框架公约,但确实增加了灵活性另一层更多的工作。

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. This is to perform a Page Redirect to the View returned by the 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.

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. currentID is set to 1 as expected.

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. I also tried JSON.Stringify before passing the array e.g.

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

Again the id array ends up null on the controller side.

Does anyone have any pointers on getting the int array to pass to the controller correctly? 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.

Thanks!

解决方案

To pass an array of simple values in MVC, you simply need to give the same name to multiple values e.g. The URI will end up looking something like this

/{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.

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 e.g.

/{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. 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 e.g.

/{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. 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.

这篇关于传递一个int数组MVC控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆