如何在MVC中使用Ajax在FormCollection中获取radioButton值

时间:2019-02-08 01:35:19

标签: javascript jquery ajax

嗨,我有一个动作方法,该方法具有参数FormCollection。在表单中,有两个单选按钮,如下所示:

@Html.RadioButton("radioSource", "Yes", true, new { id = "radioMoveAll", data_toggle = "tooltip", data_placement = "right", title = "Click to select", htmlAttributes }) Yes
@Html.RadioButton("radioSource", "No", false, new { id = "radioMoveAll", data_toggle = "tooltip", data_placement = "right", title = "Click to select", htmlAttributes })  No

我的操作方法如下,通过在Ajax代码中进行序列化将表单集合作为参数传递。

 public ActionResult StructureMove(FormCollection  form)
    {
        Structure sourceStructure = new Structure();
        sourceStructure = (Structure)HttpContext.Session["Return-Source-Structure"];


        HttpContext.Session["Form"] = form;
        HttpContext.Session["Source-Structure"] = sourceStructure;                       
        string selectedNode = sourceStructure.Code;
        string returnStructure = "";           

        StructureToMove stm = (StructureToMove)HttpContext.Session["StructureMoveObject"];
        var form1 = (FormCollection)HttpContext.Session["Form"];

        if (HttpContext.Request.Form["radioSource"].HasValue())
        {
            stm.MoveAll = HttpContext.Request.Form["radioSource"]=="Yes"?true:false;
        }
        if (HttpContext.Request.Form["chkreturnToSource"].HasValue())
        {
            stm.ReturnToDestination = HttpContext.Request.Form.GetValues("chkreturnToSource")[0] == "true" ? true:false;
        }
        .....
    }

我的ajax代码通过Ajax将表单作为数据传递来执行MoveController Acton方法。

 $('#btnmoveStructure').unbind('click').click(function () {    

var $form = $('#frmStructureMove');
var moveData = $form.serialize();
moveData = $('#frmStructureMove').serialize();
alert(moveData);

var winMoveStructure = $("#MoveStructureModalWindow").data("kendoWindow");


$.ajax({
    type: 'POST',
    async: true,
    cache: false,        
    url: '/StructureMove/StructureMove',
    // dataType:"json",
    //data: { "model": moveData, "form": moveData },        
    data: { "form": moveData },        
    success: function (results) {
        $('#btnmoveStructure').hide();           
        $('#MoveStructureModalWindow').html(results);
        console.log("Move window results in HTML ");

    },
    complete: function () {

    },
    done: function () {

    },
    error: function (jqXHR, exception) {
        var msg = "";
        if (jqXHR.status === 0) {
            msg = 'Not connect.\n Verify Network.';
        } else if (jqXHR.status === 404) {
            msg = 'Requested page not found. [404]';
        } else if (jqXHR.status === 500) {
            msg = 'Internal Server Error [500].';
        } else if (exception === 'parsererror') {
            msg = 'Requested JSON parse failed.';
        } else if (exception === 'timeout') {
            msg = 'Time out error.';
        } else if (exception === 'abort') {
            msg = 'Ajax request aborted.';
        } else {
            msg = 'Uncaught Error.\n' + jqXHR.responseText;
        }
        console.log("failure : " + msg);
    }
});

如何获取动作方法中单选按钮的值。当前的Request.Form [“ radioSource”]确实返回“是”,这是错误的。“加载时”单选按钮始终设置为“是”值。

运行表单[0]时返回以下值

hdnStructureCode = 0000641338&__ RequestVerificationToken = MqBNaHfDzuRRkkvyp4tS716Fr4nRhlTANBASp3rJQYB2E1lpKLN40UuTW9e6a3caYIa2cCHCueouZbrKT7tcjc1mexSysWoXBi8P_J6gN_41&代码= 0000641338&描述=电池+字符串%2C + VRLA%2C +世纪%2FYuasa%2C + UXH125-6%2C + 6.00 + V%2C + 110.00 + AH&的 radioSource =是&radioDestination =&DestinationCode = DCP-SS-00001922&DestinationDescription = Batteries&chkReturnToSource = true&chkReturnToSource = false

我已突出显示radioSource值(是),但是如何在MVC C3动作方法中进行检索。请在尝试所有可能的方法时卡住。我不能使用键值,因为它没有一个。它以queryString返回。

能否请您告诉某人该Ajax或我的C#代码在哪里修复

预先感谢

理论

1 个答案:

答案 0 :(得分:0)

单选按钮的值可能未正确发布,可能是因为您有两个具有相同ID的元素。尝试为两个单选按钮都提供唯一的ID

unsigned long flashCount = 0;
bool bSwitch = true;

void setup()
{
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
}

void loop()
{
  if((flashCount + 1000) < millis())
  {
    if(bSwitch)
    {
      analogWrite(6, 0);
      analogWrite(7, 1);
      bSwitch = false;
    }
    else
    {
      analogWrite(7, 0);  
      analogWrite(6, 1);
      bSwitch = true;
    }
    flashCount = millis();
  }
}

至少值得一试。无论哪种方式,都绝对不能让两个元素具有相同的ID。