Asp.net mvc asp.net从几个文本字段获取值
我在收集文本字段中的值时遇到问题。 在我的屏幕上的每一行我都有一个文本字段,用户应该在那里添加值(你可以从图片中看到) 这是我的html代码:Asp.net mvc asp.net从几个文本字段获取值,asp.net-mvc,Asp.net Mvc,我在收集文本字段中的值时遇到问题。 在我的屏幕上的每一行我都有一个文本字段,用户应该在那里添加值(你可以从图片中看到) 这是我的html代码: @使用(Html.BeginForm(“创建”、“用户”、FormMethod.Post)) { @Html.AntiForgeryToken() @Html.DisplayNameFor(model=>model.BarCode) @DisplayNameFor(model=>model.Name) @DisplayNameFor(model=>m
@使用(Html.BeginForm(“创建”、“用户”、FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.DisplayNameFor(model=>model.BarCode)
@DisplayNameFor(model=>model.Name)
@DisplayNameFor(model=>model.profice)
数量
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.BarCode)
@DisplayFor(modelItem=>item.Name)
@DisplayFor(modelItem=>item.Profit)
数量:
}
}
本节将在现有表单(嵌套表单)内创建表单,这不是绑定文本框的好方法:
<form action="/action_page.php">
Amount:
<input type="text" id="amountTag" name="Amount" maxlength="2" placeholder="0" size="4" runat="server" />
</form>
2) 使用for
循环将模型内部视图与HTML帮助程序绑定。在这里,您应该为Amount
属性添加TextBoxFor
helper,以允许用户使用以前的HTML属性输入数值:
@model IEnumerable<ViewModelClassName>
@using (Html.BeginForm("Create", "User", FormMethod.Post))
{
@Html.AntiForgeryToken()
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.BarCode)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Profit)
</th>
<th>
Amount
</th>
</tr>
@for (int i = 0; i < Model.Count; i++)
{
@Html.HiddenFor(modelItem => modelItem[i].Id)
<tr>
<td>
@Html.DisplayFor(modelItem => modelItem[i].BarCode)
</td>
<td>
@Html.DisplayFor(modelItem => modelItem[i].Name)
</td>
<td>
@Html.DisplayFor(modelItem => modelItem[i].Profit)
</td>
<td>
@* Amount *@
@Html.LabelFor(modelItem => modelItem[i].Amount)
@Html.TextBoxFor(modelItem => modelItem[i].Amount, new { maxlength = 2, ... })
</td>
</tr>
}
<tr>
<td><input type="submit" value="Submit" class="btn btn-default" /></td>
</tr>
</table>
}
从这一点来看,您的模型绑定应该可以正常工作
相关问题:嵌套表单不是一个好主意-您可以创建
Amount
属性,使用for
循环并像@Html.TextBoxFor(modelItem=>item[i].Name)那样绑定它,然后添加viewmodel类作为后期操作参数。请给出一些示例:)
@model IEnumerable<ViewModelClassName>
@using (Html.BeginForm("Create", "User", FormMethod.Post))
{
@Html.AntiForgeryToken()
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.BarCode)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Profit)
</th>
<th>
Amount
</th>
</tr>
@for (int i = 0; i < Model.Count; i++)
{
@Html.HiddenFor(modelItem => modelItem[i].Id)
<tr>
<td>
@Html.DisplayFor(modelItem => modelItem[i].BarCode)
</td>
<td>
@Html.DisplayFor(modelItem => modelItem[i].Name)
</td>
<td>
@Html.DisplayFor(modelItem => modelItem[i].Profit)
</td>
<td>
@* Amount *@
@Html.LabelFor(modelItem => modelItem[i].Amount)
@Html.TextBoxFor(modelItem => modelItem[i].Amount, new { maxlength = 2, ... })
</td>
</tr>
}
<tr>
<td><input type="submit" value="Submit" class="btn btn-default" /></td>
</tr>
</table>
}
[HttpPost]
public ActionResult Create(List<ViewModelName> model)
{
try
{
// example to select all amount values
var x = model.Select(x => x.Amount).ToList();
// do something
return RedirectToAction("Index");
}
catch
{
// error handling
return View(model);
}
}