- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到了这个问题,我尝试进行 AJAX 调用以将用户在网页上输入的数据发布到 Controller 方法。数据永远不会到达 Controller ,它总是会抛出错误。问题可能是 Controller 期望的数据与 AJAX 调用发送给它的数据类型不同,但我真的不知道期望的数据应该是什么样子。
这是构建数组的 JS/jQuery,它是表格单元格值数组的数组:
var i = 0;
var cells = new Array(); // cell values in one column
var rows = new Array(); // list of columns (rows)
$('#datatable').find('tr').each(function () {
$('td :not([type="button"],[id="Nr."])', this).each(function () {
cells.push($(this).val());
});
if (i > 0) {
rows.push(cells);
cells = [];
}
i++;
});
这是 AJAX 调用的样子:
rows = JSON.stringify({ 'rows': rows });
$.ajax({
url: "/Home/Q_FourthPage",
type: "POST",
datatype: "json",
traditional: true,
data: rows,
success: function (response) {
alert("Success");
console.log("Sucess ", response);
},
error: function (response) {
alert("Error");
console.log("Error ", response);
}
});
Controller 仍然是空的 - 它应该获取什么参数才能使数据作为字符串/整数/其他数组的数组到达?谢谢。
编辑:这是 Q_FourthPage Controller 方法使用的 View 模型:
public class Q4_Answer_VM
{
public Models.User User { get; set; }
public List<Models.Capacity> Matrix { get; set; } //this is where the data should go
//a capacity describes one row of the table
public Models.QuestionText TMatrix { get; set; }
}
容量类如下所示 - 它是表格的一行。
public class Capacity
{
// This class is a model for the capacity matrix. it represents one column.
//PK
public int ID { get; set; }
//FK
public int Questionnaire_ID { get; set; }
//Values
public String Load { get; set; }
public String Source { get; set; }
public String Goal { get; set; }
public String Frequency { get; set; }
public String Distance { get; set; }
public virtual Questionnaire Questionnaire_ { get; set; }
}
这是 Controller 方法的头部:
[HttpPost]
public ActionResult Q_FourthPage(ViewModels.Q4_Answer_VM vm)
这些类中有很多属性 - 这就是调用失败的原因吗?
编辑 2: View
这是 View :
@model MyANTon.ViewModels.Q4_Answer_VM
@{
ViewBag.Title = "myANTon Anforderungserfassung";
ViewBag.HideNavBar = false;
}
@using (Html.BeginForm(Html.BeginForm("Q_FourthPage", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })))
{
<div class="container">
<div align="center">
<ol class="breadcrumb" align="center" text-align="center">
<li class="breadcrumb-item">@Html.ActionLink("Lasten", "Q_Firstpage", "Home", new { @class = "elements" }, null)</li>
<li class="breadcrumb-item">@Html.ActionLink("Lastaufnahme-/abgabe", "Q_Secondpage", "Home", new { @class = "elements" }, null)</li>
<li class="breadcrumb-item">@Html.ActionLink("Weitere Anforderungen", "Q_Thirdpage", "Home", new { @class = "elements" }, null)</li>
<li class="breadcrumb-item"><b><u>@Html.ActionLink("Kapazitäten", "Q_Fourthpage", "Home", new { @class = "elements" }, null)</u></b></li>
<li class="breadcrumb-item">@Html.ActionLink("Auftragserzeugung", "Q_Fifthpage", "Home", new { @class = "elements" }, null)</li>
</ol>
</div>
<div class="jumbotron">
<div>
@Html.TextBox("file", "", new { type = "file" }) <br />
<input type="submit" value="Upload" />
@ViewBag.Message
</div>
<h1>4. Kapazitätsbetrachtung</h1>
<p>Wie viele Fahrzeuge werden benötigt? Um dies auszurechnen wird eine Transportmatrix benötigt.</p>
<p>Ein Beispiel einer Transportmatrix ist in Tabelle 1 zu sehen.</p>
<p>Um die Anzahl der Roboter zu berechnen ist auch eine Angabe der Produktionsschichten relevant:</p>
<ul>
<li>In wie vielen Schichten läuft die Fertigung?</li>
</ul>
<p>
Um mögliche Engpässe oder Umwege zu betrachten ist ein Layout der Produktionsstätte wie in Abbildung 3 von Vorteil.
Idealerweise mit Angaben über andere Verkehrsteilnehmer (Personenverkehr, Staplerverkehr, Kreuzungen).
</p>
<input type="button" id="BtnPlus" name="BtnPlus" class="BtnPlus" value="Zeile hinzufügen (+)" align="center" style="vertical-align: middle" />
<table class="grid" id="datatable" style="table-layout:fixed">
<thead>
<tr style="text-align: center" id="header">
<th style="vertical-align: middle" id="Nr.">Nr.</th>
<th>Last</th>
<th>Quelle</th>
<th>Ziel</th>
<th>Frequenz [/h]</th>
<th>Abstand [m]</th>
<th>Zeile löschen</th>
</thead>
<tbody></tbody>
</table>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" name="Speichern" value="Speichern" id="BtnSave" />
<input type="submit" class="btn btn-default" name="Speichern und weiter" value="Speichern und weiter" />
<input type="button" class="btn btn-default" value="Weiter zu Schritt 5" onclick="@("window.location.href='" + @Url.Action("Q_Fifthpage", "Home") + "'");" />
</div>
</div>
</div>
</div>
}
<script src="~/Scripts/jquery-3.2.1.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.16/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.js"></script>
@Scripts.Render("~/bundles/datatables")
编辑编号3:由于下面的用户derloopkat提供的解决方案,它现在可以工作了。作为提示:Ajax 和 Forms 似乎不能很好地结合在一起。我不得不将“保存”按钮移到表单之外 (@using...{})。
最佳答案
这成功了。问题是在 json 中创建一个与模型中的确切名称和结构匹配的对象。您不需要在 json 中包含所有成员,但如果您希望能够在服务器上检索它们的值,那么出现的成员应该与模型匹配。
var capacities = new Array(); // cell values in one column
$('#datatable').find('tr').each(function () {
var cells = $(this).find('td :not([type="button"],[id="Nr."])');
var capacity = {
Load: cells[0].value,
Source: cells[1].value,
Goal: cells[2].value,
Frequency: cells[3].value,
Distance: cells[4].value
};
capacities.push(capacity);
});
在您的 Ajax 调用中,dataType 是您希望从服务器获得的类型,而不是您发送的类型(即 contentType)。
var model = JSON.stringify({ 'Matrix': capacities });
$.ajax({
url: "/Home/Q_FourthPage",
type: "POST",
contentType: "application/json; charset=utf-8",
traditional: true,
data: model,
success: function (response) {
alert("Success");
console.log("Sucess ", response);
},
error: function (response) {
alert("Error");
console.log("Error ", response);
}
});
我的表格是虚拟的 Html。
<table id="datatable">
<tr>
<td><input type="text" value="11" /></td>
<td><input type="text" value="12" /></td>
<td><input type="text" value="13" /></td>
<td><input type="text" value="14" /></td>
<td><input type="text" value="15" /></td>
</tr>
<tr>
<td><input type="text" value="21" /></td>
<td><input type="text" value="22" /></td>
<td><input type="text" value="23" /></td>
<td><input type="text" value="24" /></td>
<td><input type="text" value="25" /></td>
</tr>
</table>
Action 方法是:
[HttpPost]
public ActionResult Q_FourthPage(ViewModels.Q4_Answer_VM vm)
{
return View("Index");
}
如果你打算从这里返回json,那么你需要在Ajax调用中添加dataType: 'json'
。
关于javascript - ASP MVC,Ajax : return array of array of integers to controller method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48095456/
我正在尝试执行 JavaPairRDD 和 JavaPairRDD 的 leftOuterJoin> 并且函数签名返回类型是 JavaPairRDD>>> 这里可选的是 com.google.comm
我正在尝试按元素的频率对元素进行排序 import java.io.BufferedReader; import java.io.IOException; import java.io.InputSt
这个问题已经有答案了: Is List a subclass of List? Why are Java generics not implicitly polymorphic? (19 个回答) 已
编辑:问题已解决:请参阅 Karim SNOUSSI 的答案和我在下面的评论。 这是我在堆栈溢出时遇到的第一个问题,所以我可能不会一开始就把所有事情都做对。对此感到抱歉。此外,我对 Java 和一般
#include #include using namespace std; class Integer { public: int i; Integer (int ll
我不明白: ArrayList list = new ArrayList(); Collection list1 = new ArrayList(); 类 ArrayList扩展实现接口(interf
我编写了:。它成功了。我不知道为什么?
我编写了:。它成功了。我不知道为什么
我编写了:。它成功了。我不知道为什么?
Collectors.counting()返回 long此方法中每个键的值: private static Map countDuplicates(HashSet cards) { retur
我正在尝试通过搜索旧元素并将其替换为新元素来更新节点的元素。但是有一个我不明白的错误。是什么导致我的代码出现该错误,我该如何解决?错误; The method update(Integer, Inte
我有一个称为 client 的表,其中有一列称为created_time ,所以实际上我想绘制一个 map ,以便我可以知道在哪一年和哪一个月添加了多少客户?现在的要求是假设在 2018 年 11 月
这个问题已经有答案了: Is Java "pass-by-reference" or "pass-by-value"? (91 个回答) 已关闭 8 年前。 我对 ArrayList Collecti
我意识到下面的代码是正确的 Integer.MIN_VALUE == -Integer.MIN_VALUE == Math.abs(Integer.MIN_VALUE) 这是因为当我们取反-21474
我有以下类 AccountWebappGridRow,它扩展了 AccountGridRow: public class AccountWebappGridRow extends AccountGri
我正在学习 Haskell 并看到了函数组合。 尝试复合 map和 foldl mapd = (map.foldl) 比 test = (mapd (\x y -> x + y ) [1,2,3,4]
我有两个相同大小的数组和两个方法。 public class Client { private static int[] ints; private static final int
我喜欢 Java 8 中的 Streams 概念。现在我想借助 Java Streams 将 Java 中的 Map 转换为排序列表。我只想显示列表而不将其存储在任何地方。我希望在结果列表中有这个输出
我有一个数据库表,其中包含电视节目类型列表和关联的 ARGB 颜色值,用于在显示电视指南时突出显示 Android ListView 中的电视节目。流派表看起来像这样... id genre
我有一个 Integer 类,它应该模拟一个整数 mod n。因此,它具有如下构造函数: Integer::Integer(int x) : m(x), n(0) { } Integer::I
我是一名优秀的程序员,十分优秀!