【发布时间】:2018-04-29 13:44:44
【问题描述】:
我正在使用 C# ASP.NET MVC 5 使用 Bootstrap 和 jquery 开发应用程序。我对 Javascript 很陌生。
1。如何更改 javascript 以仅允许“数量”坐标数? (找到方法并更新了代码)
- 如何将“提交”上的“列表”发布给控制器?我猜相应的模型会有一个 x 和 y 的列表,我将列表绑定到该列表?
谢谢!
控制器
[HttpGet]
public ActionResult Index() {
var plate = new Plate() {
Holes = new Holes() {
Coordinates = new List < Tuple < double, double >> ()
},
};
return View(plate);
}
[HttpPost]
public ActionResult Index(Plate plate) {
try {
// access plate.Holes.Coordinates
DrawingGenerator drawingGenerator = new DrawingGenerator(plate);
string outputFileName = drawingGenerator.GenerateDrawing();
ViewBag.fileName = outputFileName;
return View("../Download/Success");
} catch (Exception) {
return View("ErrorPage");
throw;
}
}
cshtml 文件
<div class="tab-pane fade" id="tab2">
<div class="form-inline col-md-5">
@Html.TextBox("Quantity", null, new { @class = "form-control" })
@Html.TextBox("X", null, new { @class = "form-control" })
@Html.TextBox("Y", null, new { @class = "form-control" })
<input type="button" id="Add" class="button1" value="Add" />
</div>
<ul id="list"></ul>
</div>
<script type="text/javascript">
$(document).ready(function () {
// list container
var listContainer = $('#list');
var count = 0;
$("#Add").click(function () {
// get x & y from tb
var qty = $("#Quantity").val();
if (count == qty)
{
alert("can't add more! update quantity");
return;
}
var x = $("#X").val();
var y = $("#Y").val();
// add new list item
listContainer.prepend('<li> (' + x + ', ' + y + ') </li>');
count = count + 1;
// clear value input
$('#X').val('');
$('#Y').val('');
});
});
【问题讨论】:
-
"只允许'数量'个坐标?"你能详细说明一下吗?另外还要解释“@Html.TextBox”;我以前没见过这个;
-
感谢您的回复。我的意思是,我应该只能使用“数量”文本框中指定的那些坐标。 @Html.TextBox 是 Razor 语法。无论如何,我更新了相同的代码。
-
你可以在这里添加你的控制器代码
-
@JephrenNaicker 添加了控制器的代码
-
这里有一些链接可以帮助Passing A List Of Objects Into An MVC Controller Method Using jQuery Ajax,[如何将 List
传递给 MVC 4 中的控制器](stackoverflow.com/questions/34221948/…) 尽管我建议将 x, y 保存在 Parrell数组Post JavaScript array with AJAX to asp.net MVC controller
标签: javascript jquery-ui bootstrap-4