![](/img/trans.png)
[英]Making an AJAX request to an MVC controller and getting the data from the response in js
[英]Controller not getting information (for the Model) sent from Ajax request
我从 8822996504788 数组中的 HTML 表中获取所有信息,我通过 Ajax 请求将其发送到 Controller。查看代码:(已添加)
<table id="tblAuction" class="table table-striped" style="width:80%" align="center">
<thead>
<tr>
<th style="width:150px">Image</th>
<th style="width:150px">Goods ID</th>
<th style="width:150px">Description</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var items in Model.tbl_Goods)
{
<tr>
<td>
@if (items.fileTobePosted != null)
{
<img src="@items.fileTobePosted" width="100" height="100" />
}
else
{ <label>No picture</label>
}
</td>
<td>@items.GoodsInformation<
<td><input type="button" value="Remove" onclick="Remove(this)" /></td>
</tr>
}
</tbody>
<tfoot>
<tr>
<td><input name="fileTobePosted" type="file" id="fileTobePosted"></td>
<td><input type="text" id="txtGoodsInformation" /></td>
<td><input type="text" id="txtDescription" /></td>
<td><input type="button" id="btnAdd" value="Add" /></td>
</tr>
</tfoot>
</table>
这是我的 JS 和 Ajax 请求:
//js code to get all textboxes values
$("body").on("click", "#btnSave", function () {
var formdata = new FormData($('form').get(0));
var customers = new Array();
$("#tblAuction TBODY TR").each(function () {
var row = $(this);
var customer = {};
customer.GoodsInformation = row.find("TD").eq(1).html();
customer.Description = row.find("TD").eq(2).html();
customers.push(customer);
});
formdata.append("Goods", JSON.stringify(customers));
$.ajax({
type: "POST",
url: "@Url.Action("test","Home")",
data: formdata,
processData: false,
contentType: false,
dataType: "json"
});
});
还有我想从中获取信息的 controller。
public ActionResult Test(tbl_Goods Goods)
{
HttpPostedFileBase fileTobePosted = Request.Files["fileTobePosted"];
var getdata = Request.Form["auctionGoods"];
我在 filetobeposted 中得到了文件,这很好。 我也得到信息
var getdata
它恭敬地显示:{goodsinformation, description}。
但我想遍历它,因为用户可能会添加一行或多行。 像这样。
if (fileTobePosted != null)
{
//Loop through the forms
for (int i = 0; i <= Request.Form.Count; i++)
{
var GoodsInformation = Request.Form["GoodsInformation[" + i + "]"];
var Description = Request.Form["Description[" + i + "]"];
但是 Goodsinformation and Description 在这里返回 NULL。
此外,当鼠标悬停在我的 model 上时,我得到 NULL
public ActionResult Test(tbl_Goods Goods)
我尝试了您的代码片段,但缺少一些我自己填写的内容。 index.html
<html>
<head>
</head>
<body>
<button id="btnSave">Save</button>
<table id="tblAuction">
<tbody>
<tr>
<td></td>
<td>Info1</td>
<td>Descr1</td>
</tr>
<tr>
<td></td>
<td>Info2</td>
<td>Descr2</td>
</tr>
</tbody>
</table>
<script src="lib/jquery/dist/jquery.min.js"></script>
<script>
$("body").on("click", "#btnSave", function () {
var customers = new Array();
$("#tblAuction TBODY TR").each(function () {
var row = $(this);
var customer = {};
customer.GoodsInformation = row.find("TD").eq(1).html();
customer.Description = row.find("TD").eq(2).html();
customers.push(customer);
});
$.ajax({
type: "POST",
url: "Home/Test",
data: JSON.stringify(customers),
contentType: "application/json; charset=utf-8",
dataType: "json"
});
});
</script>
</body>
</html>
然后是 Homecontroller.cs:
using Microsoft.AspNetCore.Mvc;
public class Goods
{
public string GoodsInformation { get; set; }
public string Description { get; set; }
}
[Route("[controller]/[action]")]
public class HomeController : Controller
{
[HttpPost]
public IActionResult Test([FromBody] List<Goods> goods)
{
return Ok(goods);
}
}
当我按下保存按钮时,我在商品参数中得到了两条 TR 行。 我不确定您为什么要尝试创建一个表单数据 object,也不确定您为什么要尝试访问 controller 中的 HttpPostedFileBase。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.