form.serialize不适用于字符串列表

时间:2018-11-05 22:42:47

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

我正在尝试使用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!!");
    }
});

});

这似乎也不起作用。

1 个答案:

答案 0 :(得分:0)

{}不是必需的,请删除它们,仅保留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>