简体   繁体   English

如何将var Array分配给字符串Array jQuery? MVC3剃刀

[英]How assign var Array to string Array jQuery? MVC3 Razor

I have the next code of jQuery: 我有jQuery的下一个代码:

function getParameter() {
    var valor = [];
    $("input[name='TiposServicioSeleccionados']:checked").each(function (i) {
        valor.push($(this).val());
    });
    if (valor.length == 0) {
        valor.push("0");
    }

    return {
        nofacturados: $("#nofacturados:checked").val(),
        precioactual: $("#precioactual:checked").val(),
        TiposServicioSeleccionados: valor
    };
}

I want to assing 'valor' to 'TiposServicioSeleccionados', in my controller I have this 我想将“勇气”赋予“ TiposServicioSeleccionados”,在我的控制器中,我有这个

public ActionResult LeerExt_DevolucionRepuesto(string[] TiposServicioSeleccionados, bool? facturados, bool? nofacturados)
        {
            int id_empresaservicio = Convert.ToInt16(Session["id_empresaservicio"]);
            var res = GetDevolucionRepuestos(TiposServicioSeleccionados,facturados,nofacturados);

            return Json(res.ToDataSourceResult(request));
        }

but always TiposServicioSeleccionados is null, I don't know why... 但是TiposServicioSeleccionados总是为空,我不知道为什么...

I call getParameter in: 我在以下方式调用getParameter

Read(read => read.Action("LeerExt_DevolucionRepuesto", "Consultas").Data("getParameter"))

I need, For example, if I checked three checkbox, in TiposServicioSeleccionados I want to save the next values: TiposServicioSeleccionados[0]="value of the first chekbox" , TiposServicioSeleccionados[1]="value of the second chekbox" , TiposServicioSeleccionados[2]="value of the third chekbox" 我需要,例如,如果我选中了三个复选框,则在TiposServicioSeleccionados我要保存下一个值: TiposServicioSeleccionados[0]="value of the first chekbox" TiposServicioSeleccionados[1]="value of the second chekbox" TiposServicioSeleccionados[0]="value of the first chekbox"TiposServicioSeleccionados[1]="value of the second chekbox" TiposServicioSeleccionados[2]="value of the third chekbox" TiposServicioSeleccionados[1]="value of the second chekbox"TiposServicioSeleccionados[2]="value of the third chekbox"

Regards 问候

Try with JSON.stringify and use push for adding array objects 尝试使用JSON.stringify并使用push添加数组对象

function getParameter() {
 var valor = [];
 $("input[name='TiposServicioSeleccionados']:checked").each(function (i) {
    valor.push($(this).val());
 });
 if (valor.length == 0) {
    valor.push("0");
 }

return {

    TiposServicioSeleccionados: JSON.stringify(valor)
};

} }

Your code works just fine, take a look at this fiddle: 您的代码工作正常,请看一下这个小提琴:
http://jsfiddle.net/tXAn6/ http://jsfiddle.net/tXAn6/

I added 3 checkboxes and manually selected the first and the third one. 我添加了3个复选框,并手动选择了第一个和第三个。

... value="Servicio A" checked /> A
... value="Servicio B" /> B
... value="Servicio C" checked /> C

The result is: 结果是:

Servicio A, Servicio C

Edit: 编辑:

TiposServicioSeleccionados should not be Capitalize, as it is a variable it should start with downcase tiposServicioSeleccionados . TiposServicioSeleccionados不应大写,因为它是一个变量,应以小写的tiposServicioSeleccionados

By convection: functionNamesLikeThis , variableNamesLikeThis , ClassNamesLikeThis , EnumNamesLikeThis , methodNamesLikeThis , and SYMBOLIC_CONSTANTS_LIKE_THIS . 通过对流: functionNamesLikeThisvariableNamesLikeThisClassNamesLikeThisEnumNamesLikeThismethodNamesLikeThisSYMBOLIC_CONSTANTS_LIKE_THIS

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

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