【问题标题】:form.serialize not working for a list of stringsform.serialize 不适用于字符串列表
【发布时间】:2018-11-05 22:42:47
【问题描述】:

我正在尝试使用 AJAX 将值从客户端传递到服务器端。我想将整个模型作为参数传递,所以我将它序列化并作为参数传递。但是它似乎不起作用,并且在服务器上找不到任何值。

字符串列表基本上是一个select2下拉选择值。

这是简化的代码版本:

型号:

public class FruitsViewModel
{
    public List<SelectListContainer> FruitList { get; set; }
    public List<SelectListContainer> VegetableList { get; set; }
    public List<string> SearchFruit { get; set; }
    public List<string> SearchVegetable { get; set; }
} 

查看:

<form id="formFruits">

    @Html.ListBoxFor(m => m.SearchFruit , new SelectList(Model.FruitList , "Value", "Display", Model.SearchFruit ), new { @class = "form-control", id = "SearchFruit ", name = "SearchFruit ", multiple = "true" })

    @Html.ListBoxFor(m => m.SearchVegetable , new SelectList(Model.VegetableList , "Value", "Display", Model.SearchVegetable ), new { @class = "form-control", id = "SearchVegetable ", name = "SearchVegetable ", multiple = "true" })

    <button id ='btnSubmit' type='button'>Submit</button>

</form>

脚本:

$(document).on("click", "#btnSubmit", function (e) {
e.preventDefault();
var page = 4;
var url = "/FruitDetails/Index";
$.ajax({
    url: url,
    type: 'POST',
    contentType: 'application/json',
    dataType: "json",
    cache: false,
    data: { model: $('#formFruits').serialize(),page:page},
    success: function (result) {
        $("#divResults").html(result);
    },
    error: function (result) {
        console.log("Failure!!");
    }
});
});

控制器:

[HttpPost]
public ActionResult Index(FruitsViewModel model,int page)
{
}

代码有问题吗?还是 Select2 下拉列表的已知问题?

编辑:我也尝试直接分配它们并将它们作为 JSON 字符串发送,但这也不起作用。

$(document).on("click", "#btnSubmit", function (e) {
e.preventDefault();
var page = 4;
var url = "/FruitDetails/Index";

var SearchFruit = $("#SearchFruit ").val(); 
var SearchVegetable = $("#SearchVegetable").val();

var model = JSON.stringify({
    SearchFruit : SearchFruit,
    SearchVegetable : SearchVegetable
});

$.ajax({
    url: url,
    type: 'POST',
    cache: false,
    contentType: 'application/json',
    dataType: "json",
    data: { model:model, page:page },
    success: function (result) {
        $("#divResults").html(result);
    },
    error: function (result) {
        console.log("Failure!!");
    }
});

});

这似乎也不起作用。

【问题讨论】:

  • 为了调试方便,你能不能把它改成Index(object model),看看model里面是什么?
  • 只要是data: $('#formFruits').serialize(),
  • @StephenMuecke 我最初错过了添加参数。请检查修改后的代码。
  • 您不能在 javascript 对象中使用 .serialize()。删除contentType: 'application/json',及其data: $('#formFruits').serialize() + '&amp;page=' + page,.serialize()创建查询字符串格式)
  • 而第二个例子不起作用,因为整个对象需要被字符串化,而不是model - var model = { SearchFruit : SearchFruit, SearchVegetable : SearchVegetable };(未字符串化)和data: JSON.stringify({ model:model, page:page }),

标签: c# ajax asp.net-mvc jquery-select2 form-submit


【解决方案1】:

{} 不是必须的,删除它们,只留下data: $ ('#fromFruits').Serialize() 和表格,这样做:

使用“提交”,一般用于验证表单

$( "#formFruits" ).on( "submit", function( event ) {
  event.preventDefault();
  
  data = $( this ).serialize()
  url = $(this).attr("action")
  
  $.ajax({
    url: url,
    type: 'POST',
    cache: false,
    data: data,
    success: function (result) {
        $("#Result").html(result);
    },
    error: function (result) {
    $("#Result").html("Failure");
    }
});
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="formFruits" action="/action_page.php">

  First input:<br>
  <input type="text" name="input1" required><br>
  
  <button type='submit'>Submit</button>
</form>

<a> Result: </a>
<a id="Result"> </a>

【讨论】:

  • 我需要点击按钮,所以不完全提交这个。
猜你喜欢
  • 2018-06-21
  • 2015-07-25
  • 1970-01-01
  • 2022-12-02
  • 2017-01-06
  • 1970-01-01
  • 2016-05-22
  • 1970-01-01
  • 2017-03-25
相关资源
最近更新 更多