【问题标题】:asp.net mvc jquery posting multiple hidden fieldsasp.net mvc jquery 发布多个隐藏字段
【发布时间】:2014-03-29 04:15:21
【问题描述】:

我有这个隐藏字段列表

   <input type="hidden" value="1" id="serviceslist" name="serviceslist" />
   <input type="hidden" value="2" id="serviceslist" name="serviceslist" />
   <input type="hidden" value="3" id="serviceslist" name="serviceslist" />

我添加了更多按钮,单击该按钮时希望将服务列表发送到 mvc 控制器。基本上我认为我需要将列表作为数组发送。我该怎么做?

  $('#addmoreservices').click(function () {          
     var serviceslist= $("#serviceslist").val()
        $.ajax({
            url: url,
            data: { serviceslist: serviceslist },
            cache: false,
            dataType: "json",
            traditional: true,
            type: "POST",
            success: function (data) {

                alert("dddd");
            },
            error: function (reponse) {
                alert("error : " + reponse);
            }
        });
    });

这是我的 mvc 控制器

 [HttpPost]
    public ActionResult AddmoreServices(List<int> serviceslist)
    {

        return View();
    }

【问题讨论】:

  • id 应该是唯一的

标签: jquery asp.net asp.net-mvc-4 asp.net-mvc-5


【解决方案1】:

试试这样的,

HTML:

<input type="hidden" value="1" class="serviceslist" name="serviceslist" />
<input type="hidden" value="2" class="serviceslist" name="serviceslist" />
<input type="hidden" value="3" class="serviceslist" name="serviceslist" />

JQUERY:

$('#addmoreservices').click(function () {          
    var serviceslist= $(".serviceslist").serialize();
    $.ajax({
        url: url,
        data: serviceslist,
        cache: false,
        dataType: "json",
        traditional: true,
        type: "POST",
        success: function (data) {

            alert("dddd");
        },
        error: function (reponse) {
            alert("error : " + reponse);
        }
    });
});

【讨论】:

    【解决方案2】:

    你的 html 和 js 有一些错误。

    首先,id必须是唯一的:

    <input type="hidden" value="1" id="val1" />
    <input type="hidden" value="2" id="val2" />
    <input type="hidden" value="3" id="val3" />
    

    第二个,jquery val() 函数获取匹配元素集合中第一个元素的当前值,而不是数组。

    第三个是关于在服务器上发布您的数据。默认jquery.ajax是你的数据在urlcontentType='application/x-www-form-urlencoded',应该改成application/json

    serviceslist = [$("#val1").val(), $("#val2").val() ,$("#val3").val()];
    $.ajax({
       url: url,
       data: serviceslist,
       contentType: 'application/json',
       ....
    

    【讨论】:

      【解决方案3】:

      或者,您可以将按钮设置为 submit 之类的,

      @using(Html.BeginForm("AddMoreServices", "Your controller name", FormMethod.Post, null)) {
      
      <input type="hidden" value="1" id="val1" name="serviceslist" />
      <input type="hidden" value="2" id="val2" name="serviceslist" />
      <input type="hidden" value="3" id="val3" name="serviceslist" />
      
      <button type="submit" value="Add More Services"/>
      
      }
      

      你的控制器动作方法可以是

      [HttpPost]
      public ActionResult AddMoreServices(FormCollection collection)
      {
          try
          {
              string[] test = collection.GetValues("serviceslist");
      
              // here test[0] will give you the value of `val1`
              // test[1] will give `val2` and test[2] will give `val3`'s values
          }
          catch (Exception ex)
          {
              return null;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多