Javascript 将逗号分隔的值传递给ASP.NET MVC操作方法
好的,我基本上有一个ajax调用,从html选择多个标记检索值。。。问题是我无法将这些值传递到我的控制器(我在控制器中得到一个空引用) 在ajax调用中选中Javascript 将逗号分隔的值传递给ASP.NET MVC操作方法,javascript,c#,jquery,asp.net,ajax,Javascript,C#,Jquery,Asp.net,Ajax,好的,我基本上有一个ajax调用,从html选择多个标记检索值。。。问题是我无法将这些值传递到我的控制器(我在控制器中得到一个空引用) 在ajax调用中选中types:$('select#opt2').val()avion:$('select#opt1').val()不是多个值,因此可以正常工作。当我alert($('select#opt2').val())时,我会得到如下值:GC、VSG、AA7。。。(它们之间用“,”分隔) 这是我的密码: AJAX $('select#opt2').chan
types:$('select#opt2').val()
avion:$('select#opt1').val()
不是多个值,因此可以正常工作。当我alert($('select#opt2').val())
时,我会得到如下值:GC、VSG、AA7。。。(它们之间用“,”分隔)
这是我的密码:
AJAX
$('select#opt2').change(function () {
$.ajax({
url: '@Url.Action("RetournerPostes", "Home", new { area = "Avion" })',
data: { avion: $('select#opt1').val(), types: $('select#opt2').val() },
type: 'POST',
dataType: 'JSON',
//Rest of code
控制器
这就是我获取变量“types”的null引用的地方
[HttpPost]
公共异步任务这是因为作为类型传递的是字符串对象,而不是字符串列表
您必须将types变量强制转换为字符串数组:
var array = string.split(',');
并将其作为数组而不是列表传递给方法:
[HttpPost]
public async Task<ActionResult> RetournerPostes(string avion, string[] types)
{
[HttpPost]
公共异步任务RetournerPostes(字符串avion,字符串[]类型)
{
这是因为作为类型传递的是字符串对象,而不是字符串列表
您必须将types变量强制转换为字符串数组:
var array = string.split(',');
并将其作为数组而不是列表传递给方法:
[HttpPost]
public async Task<ActionResult> RetournerPostes(string avion, string[] types)
{
[HttpPost]
公共异步任务RetournerPostes(字符串avion,字符串[]类型)
{
如果您的types=“GC,VSG,AA7”,那么您传递的是字符串,而不是字符串列表,因此:
public async Task<ActionResult> RetournerPostes(string avion, string types){
var myArray = types.Split(','); // split into an array
}
public async Task RetournerPostes(字符串格式,字符串类型){
var myArray=types.Split(',');//拆分为一个数组
}
如果您的types=“GC,VSG,AA7”,那么您传递的是字符串,而不是字符串列表,因此:
public async Task<ActionResult> RetournerPostes(string avion, string types){
var myArray = types.Split(','); // split into an array
}
public async Task RetournerPostes(字符串格式,字符串类型){
var myArray=types.Split(',');//拆分为一个数组
}
类型=[]
var sel=$(“#selectID”);
对于(var i=0,n=sel.options.length;itypes=[]
var sel=$(“#selectID”);
对于(var i=0,n=sel.options.length;我希望这能解决您的问题:
$('select#opt2').change(function () {
var stringArray = new Array();
stringArray =$("#opt2>option").map(function() { return $(this).text(); }).get();
var selectedValue = $('select#opt2').val();
$.ajax({
url: '@Url.Action("RetournerPostes", "Home", new { area = "Avion" })',
data: {avion: selectedValue, types: stringArray},
type: 'POST',
dataType: 'JSON',
//Rest Code
希望这能解决您的问题:
$('select#opt2').change(function () {
var stringArray = new Array();
stringArray =$("#opt2>option").map(function() { return $(this).text(); }).get();
var selectedValue = $('select#opt2').val();
$.ajax({
url: '@Url.Action("RetournerPostes", "Home", new { area = "Avion" })',
data: {avion: selectedValue, types: stringArray},
type: 'POST',
dataType: 'JSON',
//Rest Code
如果大量绑定逗号分隔值(CSV),最简单且可维护的方法是创建一个名为CommaSeparatedModelBinder的自定义模型绑定器
捕获选择的更改事件并在用户选择选项时进行Ajax调用并不常见,但这取决于您
看法
@使用(Html.BeginForm())
{
1.
2.
3.
4.
1.
2.
3.
4.
}
$('select#opt2')。更改(函数(){
var data=JSON.stringify({avion:$('select#opt1').val(),type:$('select#opt2').val());
控制台日志(数据);
$.ajax({
url:'@url.Action(“RetournerPostes”,“Home”),
数据:数据,
键入:“POST”,
contentType:“应用程序/json”,
数据类型:“JSON”,
成功:功能(msg){
控制台日志(msg);
}
});
});
CommaSeparatedModelBinder
公共类CommaSeparatedModelBinder:DefaultModelBinder
{
私有静态只读方法info ToArrayMethod=typeof(可枚举).GetMethod(“ToArray”);
公共重写对象BindModel(ControllerContext ControllerContext,ModelBindingContext bindingContext)
{
返回BindCsv(bindingContext.ModelType、bindingContext.ModelName、bindingContext)
??基本绑定模型(controllerContext、bindingContext);
}
受保护的重写对象GetPropertyValue(ControllerContext ControllerContext、ModelBindingContext bindingContext、System.ComponentModel.PropertyDescriptor PropertyDescriptor、IModelBinder propertyBinder)
{
返回BindCsv(propertyDescriptor.PropertyType、propertyDescriptor.Name、bindingContext)
??base.GetPropertyValue(controllerContext、bindingContext、propertyDescriptor、propertyBinder);
}
私有对象BindCsv(类型、字符串名称、ModelBindingContext)
{
if(type.GetInterface(typeof(IEnumerable.Name)!=null)
{
var actualValue=bindingContext.ValueProvider.GetValue(名称);
if(实际值!=null)
{
var valueType=type.GetElementType()??type.GetGenericArguments().FirstOrDefault();
if(valueType!=null&&valueType.GetInterface(typeof(IConvertible.Name)!=null)
{
var list=(IList)Activator.CreateInstance(typeof(list).MakeGenericType(valueType));
foreach(actualValue.AttemptedValue.Split中的var splitValue(新[]{',}))
{
如果(!String.IsNullOrWhiteSpace(splitValue))
list.Add(Convert.ChangeType(splitValue,valueType));
}
如果(键入IsArray)
返回ArrayMethod.MakeGenericMethod(valueType).Invoke(这是新的[]{list});
退货清单;
}
}
}
返回null;
}
}
原始来源:如果大量绑定逗号分隔值(CSV),最简单且可维护的方法是创建一个名为CommaSeparatedModelBinder的自定义模型绑定器
捕获选择的更改事件并在用户选择选项时进行Ajax调用并不常见,但这取决于您
看法
@使用(Html.BeginForm())
{
1.
2.
3.
4.
1.
2.
3.
4.
}
$('select#opt2')。更改(函数(){
var data=JSON.stringify({avion:$('select#opt1').val(),type:$('select#opt2').val());
控制台日志(数据);
$.ajax({
url:'@url.Action(“RetournerPostes”,“Home”),
数据:数据,
键入:“POST”,
contentType:“应用程序/json”,
数据类型:“JSON”,
public class CommaSeparatedModelBinder : DefaultModelBinder
{
private static readonly MethodInfo ToArrayMethod = typeof(Enumerable).GetMethod("ToArray");
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
return BindCsv(bindingContext.ModelType, bindingContext.ModelName, bindingContext)
?? base.BindModel(controllerContext, bindingContext);
}
protected override object GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
{
return BindCsv(propertyDescriptor.PropertyType, propertyDescriptor.Name, bindingContext)
?? base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
}
private object BindCsv(Type type, string name, ModelBindingContext bindingContext)
{
if (type.GetInterface(typeof(IEnumerable).Name) != null)
{
var actualValue = bindingContext.ValueProvider.GetValue(name);
if (actualValue != null)
{
var valueType = type.GetElementType() ?? type.GetGenericArguments().FirstOrDefault();
if (valueType != null && valueType.GetInterface(typeof(IConvertible).Name) != null)
{
var list = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(valueType));
foreach (var splitValue in actualValue.AttemptedValue.Split(new[] { ',' }))
{
if (!String.IsNullOrWhiteSpace(splitValue))
list.Add(Convert.ChangeType(splitValue, valueType));
}
if (type.IsArray)
return ToArrayMethod.MakeGenericMethod(valueType).Invoke(this, new[] { list });
return list;
}
}
}
return null;
}
}