Razor @html.TextBox()未在actionlink中传入控制器
我试图从文本框中传递控制器中的值。我用谷歌搜索这个问题。但没有找到适合我的解决方案。 下面是我的控制器。 WebProductController TypeT.cshtml 当我使用按钮发送表单中的数据时,表单不识别我的方法。Razor @html.TextBox()未在actionlink中传入控制器,razor,asp.net-mvc-4,Razor,Asp.net Mvc 4,我试图从文本框中传递控制器中的值。我用谷歌搜索这个问题。但没有找到适合我的解决方案。 下面是我的控制器。 WebProductController TypeT.cshtml 当我使用按钮发送表单中的数据时,表单不识别我的方法。 我陷入了这个问题。但没有解决方案:(您的控制器应该如下所示: public ActionResult Add(IList<ShoppingItemModel> items){ foreach(ShopingItemModel item in it
我陷入了这个问题。但没有解决方案:(您的控制器应该如下所示:
public ActionResult Add(IList<ShoppingItemModel> items){
foreach(ShopingItemModel item in items){
if (item.Id != null){
ShopingItem shopingItem = service.GetById(item.Id);
... Add Item to whatever
}
}
}
您的视图(我跳过了Java脚本):
@model-IList
平板
@使用(Html.BeginForm(“Add”,“WebProductController”,FormMethod.Post)){
@Html.ValidationSummary(true)
名称
价格
...
数量
@for(int-index;indexmodel[index].Id)
@Html.DisplayFor(model=>model[index].Name)
@Html.DisplayFor(model=>model[index].Price)
@Html.DisplayFor(model=>model[index].Batch)
...
@Html.TextBoxFor(model=>model[index].Quantity)
}
}
这不是完整的解决方案,只是一个提示。
Tobitry:@using(Html.BeginForm(“Add”,“WebProduct”,FormMethod.Post))thx很多。很有效。现在你能告诉我如何发送当前模型值吗?比如名称和类型?因为我认为它也会以表单形式发送,但不会。a你试图实现一种批量创建视图吗?你可以说。我
正在尝试实现类似购物车的功能。在其中,我将向用户显示产品名称,他将l输入数量。他需要在输入数量后单击添加。现在在我的控制器中,我将发送数量、名称和类型。
@model IEnumerable<DatabaseService_WebAPI.Models.ProductType>
@{
ViewBag.Title = "Tablets";
<script type="text/javascript">
$(function () {
$('#edit').click(function() {
var name = $('#quantity').val();
this.href = this.href + '/' + encodeURIComponent(name);
});
});
</script>
}
<h2>Tablets</h2>
@*@using (Html.BeginForm("Add", "WebProductController",FormMethod.Post)) {
@Html.ValidationSummary(true)
*@
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th>
@Html.DisplayNameFor(model => model.Batch)
</th>
<th>
@Html.DisplayNameFor(model => model.Expiry)
</th>
<th>
@Html.DisplayNameFor(model => model.Quantity)
</th>
@* <th>
@Html.DisplayNameFor(model => model.Type)
</th>*@
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.Batch)
</td>
<td>
@Html.DisplayFor(modelItem => item.Expiry)
</td>
<td>
@Html.TextBox("quantity")
</td>
@Html.Hidden("idText", item.Id)
<td>
@Html.ActionLink("Add", "Add", new { id = item.Id, name=item.Name, type=item.Type }, null)
@*@Html.ActionLink("Add", "Add", null, new { id = "edit" })*@
@*<input type="submit" value="Add" />*@
</td>
</tr>
}
</table>
@*}*@
<div>
@Html.ActionLink("Back to List", "Create")
</div>
@using (Html.BeginForm("Add", "WebProductController",FormMethod.Post))
public ActionResult Add(IList<ShoppingItemModel> items){
foreach(ShopingItemModel item in items){
if (item.Id != null){
ShopingItem shopingItem = service.GetById(item.Id);
... Add Item to whatever
}
}
}
public class ShoppingItemModel{
[Required]
public Id{get;set;}
[Required]
public Amount{get;set;}
...
}
@model IList<ShoppingItemModel>
<h2>Tablets</h2>
@using (Html.BeginForm("Add", "WebProductController",FormMethod.Post)) {
@Html.ValidationSummary(true)
<table>
<tr>
<th>
Name
</th>
<th>
Price
</th>
...
<th>Amount</th>
</tr>
@for(int index; index < Model.Count; index++) {
<tr>
<td>
@Html.HiddenFor(model => model[index].Id)
@Html.DisplayFor(model => model[index].Name)
</td>
<td>
@Html.DisplayFor(model => model[index].Price)
</td>
<td>
@Html.DisplayFor(model => model[index].Batch)
</td>
...
<td>
@Html.TextBoxFor(model => model[index].Quantity)
</td>
</tr>
}
</table>
<input type="submit" value="Add" />
}