ajax - 如何将对象列表发送到我的控制器?
问题描述
我尝试将对象列表发送到控制器,但控制器始终将其接收为空。
var model= @Html.Raw(Json.Encode(Model.MultipleElements));
jQuery.ajax({
type: 'GET',
contentType: 'application/json',
url: '@Url.Action("AddField", "Flux")',
data: model,
success: function (response) {
$(".destinationMultiple").html(response);
}
});
这是我的控制器动作
public PartialViewResult AddField(List<Destination> model)
{
return PartialView("_myPartialView");
}
解决方案
您可以使用 Ajax.Beginform。如果您愿意,您可以执行以下操作,这说明了如何将数组从 View 传递到 Controller。
视图/控制器
namespace Testy20161006.Controllers
{
public class Destination
{
public string aDestination { get; set; }
}
public class TahtohViewModel
{
public List<Destination> MultipleElements { get; set; }
}
public class HomeController : Controller
{
[HttpPost]
public PartialViewResult AddField(List<Destination> MultipleElements)
{
List<String> sendout = new List<string>();
foreach (Destination dest in MultipleElements)
{
sendout.Add(dest.aDestination);
}
ViewBag.SendoutList = sendout;
return PartialView("_myPartialView");
}
public ActionResult Tut149()
{
Destination dest1 = new Destination { aDestination = "adest1" };
Destination dest2 = new Destination { aDestination = "adest2" };
Destination dest3 = new Destination { aDestination = "adest3" };
TahtohViewModel tahtoViewModel = new TahtohViewModel { MultipleElements = new List<Destination>() };
tahtoViewModel.MultipleElements.Add(dest1);
tahtoViewModel.MultipleElements.Add(dest2);
tahtoViewModel.MultipleElements.Add(dest3);
return View(tahtoViewModel);
}
看法
@model Testy20161006.Controllers.TahtohViewModel
@using Testy20161006.Controllers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Tut149</title>
<script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script type="text/javascript">
$(function () {
$("#theButton").click(function () {
var items = [];
var q = $("input[name='theElemsName']");
$.each(q, function (index, thevalue) {
var item = {};
item.aDestination = thevalue.value;
items.push(item);
});
jQuery.ajax({
type: 'POST',
contentType: 'application/json',
url: '@Url.Action("AddField", "Home")',
data: JSON.stringify(items),
success: function (response) {
//alert("success");
$(".destinationMultiple").html(response);
}
});
})
})
</script>
</head>
<body>
<div>
<table>
@{ var index = 0;}
@foreach (Destination item in Model.MultipleElements)
{
<tr><td>Item <span>@(index++)</span></td><td data-multipleelements="@(index)">@Html.TextBox(item.aDestination, null, new { Name = "theElemsName", Value = item.aDestination })</td></tr>
}
</table>
<input type="button" id="theButton" value="Add Field" />
<div class="destinationMultiple"></div>
</div>
</body>
</html>
局部视图
my partial view
@foreach (string item in ViewBag.SendoutList)
{
<div>@item</div>
}
推荐阅读
- docker - 以分离模式启动时无法连接到在 Docker 容器中运行的 Airflow
- cmd - 使用开源 puppet 运行 cmd 命令
- c++ - 有没有办法让 Visual Studio 忽略特定的 vcpkg 库?
- ruby-on-rails - Has_Many_Through 未能推动许多人通过
- java - Java Spring Webflux WebClient - 使用 HTTP2 发送 http 请求
- java - 如何使正则表达式提前匹配一位和两位数字?
- leaflet - Leaflet marker.cluster 弹出传单ajax加载的geojson数据
- wordpress - 如何在本地机器上访问 Word Press (Elementor) 代码
- javascript - 第一次在 JS 中使用 API。需要帮助将数组填充到表头的内部文本中
- heroku - 如何在 Heroku 上配置 API