Asp.net mvc 将列表作为@Html.Hidden传递,列表将被删除
我有一个问题,通过一个列表从一个表单发送到一个控制器来处理和编辑。它只是销毁列表并只通过一个空字符串。我需要维护列表,以便在控制器中添加到列表中 视图如下所示:Asp.net mvc 将列表作为@Html.Hidden传递,列表将被删除,asp.net-mvc,list,hidden-field,html.beginform,Asp.net Mvc,List,Hidden Field,Html.beginform,我有一个问题,通过一个列表从一个表单发送到一个控制器来处理和编辑。它只是销毁列表并只通过一个空字符串。我需要维护列表,以便在控制器中添加到列表中 视图如下所示: @using (Html.BeginForm("AddToBasket", "Home", FormMethod.Post)) { @Html.Hidden("product", product.Product) @Html.Hidden("basketItems", Mo
@using (Html.BeginForm("AddToBasket", "Home", FormMethod.Post))
{
@Html.Hidden("product", product.Product)
@Html.Hidden("basketItems", Model.BasketItems)
<h3>@product.Product (£@product.Price)</h3>
<div class="menu-item-quantity">
<h4>Quanitity: @Html.TextBox("quantity", 0, new { @class = "form-control-quantity" })</h4>
</div>
<input class="btn btn-lg" type="submit" value="Add to basket" />
}
@使用(Html.BeginForm(“AddToBasket”,“Home”,FormMethod.Post))
{
@Html.Hidden(“产品”,product.product)
@Html.Hidden(“basketItems”,Model.basketItems)
@产品。产品(英镑@product.Price)
数量:@Html.TextBox(“数量”,0,新{@class=“表单控制数量”})
}
控制员:
public ActionResult AddToBasket(string product, int quantity, List<string>basketItems)
{
var products = new GetProductsList<ProductLookup>().Query().ToList();
var Result = new BuyViewModel
{
Products = products,
BasketItems = basketItems.ToList()
};
return View("Buy", Result);
}
public ActionResult AddToBasket(字符串乘积、整数数量、ListbasketItems)
{
var products=new GetProductsList().Query().ToList();
var结果=新的BuyViewModel
{
产品=产品,
BasketItems=BasketItems.ToList()
};
返回视图(“购买”,结果);
}
模型:
public class BuyViewModel
{
public IList<ProductLookup> Products { get; set; }
public List<string> BasketItems { get; set; }
}
公共类BuyViewModel
{
公共IList产品{get;set;}
公共列表BasketItems{get;set;}
}
如何将该列表完整地传递给控制器???您正在寻找的是返回列表,而不是返回一个项目 一种方法是返回具有相同名称的表单元素数组,以便控制器可以将其缝合回列表中 下面是一个例子: 而不是:
@Html.Hidden("basketItems", Model.BasketItems)
添加以下内容:
for (int i = 0; i < Model.BasketItems.Count; i++)
{
@Html.HiddenFor(m => m.BasketItems[i])
}
for(int i=0;im.BasketItems[i])
}
这将生成以下html:
<input type="hidden" name="basketItems[0]" value="item 1" />
<input type="hidden" name="basketItems[1]" value="item 2" />
(也许您不需要@Html中的@,但我还没有检查)
更新
我创建了一个测试项目:
TestViewModel.cs
using System;
using System.Collections.Generic;
namespace WebApplication1.Models
{
public class TestViewModel
{
public string Name { get; set; }
public List<String> Items { get; set; }
}
}
using System.Collections.Generic;
using System.Web.Mvc;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
var viewModel = new TestViewModel {
Name = "Test name",
Items = new List<string> { "item 1" , "item 2" }
};
return View(viewModel);
}
[HttpPost]
public ActionResult Index(string name, List<string> items)
{
var viewModel = new TestViewModel
{
Name = name,
Items = items
};
return View(viewModel);
}
}
}
使用系统;
使用System.Collections.Generic;
命名空间WebApplication1.Models
{
公共类TestViewModel
{
公共字符串名称{get;set;}
公共列表项{get;set;}
}
}
HomeController.cs
using System;
using System.Collections.Generic;
namespace WebApplication1.Models
{
public class TestViewModel
{
public string Name { get; set; }
public List<String> Items { get; set; }
}
}
using System.Collections.Generic;
using System.Web.Mvc;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
var viewModel = new TestViewModel {
Name = "Test name",
Items = new List<string> { "item 1" , "item 2" }
};
return View(viewModel);
}
[HttpPost]
public ActionResult Index(string name, List<string> items)
{
var viewModel = new TestViewModel
{
Name = name,
Items = items
};
return View(viewModel);
}
}
}
使用System.Collections.Generic;
使用System.Web.Mvc;
使用WebApplication1.模型;
命名空间WebApplication1.Controllers
{
公共类HomeController:控制器
{
公共行动结果索引()
{
var viewModel=新的TestViewModel{
Name=“测试名称”,
项目=新列表{“项目1”、“项目2”}
};
返回视图(viewModel);
}
[HttpPost]
公共操作结果索引(字符串名称、列表项)
{
var viewModel=新的TestViewModel
{
Name=Name,
项目=项目
};
返回视图(viewModel);
}
}
}
Index.cshtml
@model WebApplication1.Models.TestViewModel
@{
ViewBag.Title = "Home Page";
}
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
for (int i = 0; i < Model.Items.Count; i++)
{
@Html.HiddenFor(m => m.Items[i])
}
<div class="">
@Html.TextBoxFor(m => m.Name)
</div>
<input type="submit" value="save" />
}
@model WebApplication1.Models.TestViewModel
@{
ViewBag.Title=“主页”;
}
@使用(Html.BeginForm(“Index”,“Home”,FormMethod.Post))
{
对于(int i=0;im.Items[i])
}
@Html.TextBoxFor(m=>m.Name)
}
当我单击“保存”时,表单会再次显示,但现在使用了上一个表单中的值我很恼火这个修复是多么容易。我在控制器中添加了一个检查,以在列表作为null传递时作出反应
[HttpPost]
public ActionResult Buy(string product, int quantity, List<string>BasketItems)
{
var products = new GetProductsList<ProductLookup>().Query().ToList();
**for (int i = 0; i < quantity; i++)
{
if (BasketItems == null)
{
BasketItems = new List<string> { product };
}
else
{
BasketItems.Add(product);
}
}**
var Result = new BuyViewModel
{
Products = products,
BasketItems = BasketItems
};
return View("Buy", Result);
}
[HttpPost]
公共行动结果购买(字符串产品、整数数量、ListBasketItems)
{
var products=new GetProductsList().Query().ToList();
**对于(int i=0;i
我还将视图改为使用HiddenFor而不是hidden看,我可以看到那篇文章与我的相似之处,但在那篇文章中,他们正在传递一个新的列表,而我正在尝试传递一个现有的列表。然而,想法是一样的。您的表单现在只有一个输入,但您真正想做的是添加多个隐藏输入,列表中的每一项都有一个。对不起,如果我的声音很粗,我该怎么做?没问题,以前去过:)请告诉我以下代码是否适用于您使用此方法,我通过控制器进入
basketItems.Add(产品)时出错
因为basketItems为null:/n您能看到生成的html中隐藏元素的名称吗?我在html中看不到basketItems的任何隐藏输入。我使用Hidden传递的其他项目都在html中,虽然这是完整的源代码:但它基本上是默认的ASP.NET MVC 5项目,上面的代码无限期地添加到某个地方!在玩了一番之后,将我的项目与你的项目进行了比较,我意识到我的清单已经通过了。。。。如果列表中已经有一些内容。当它是空的,它就会掉下来