如何从 Request.Form 获取所有元素值,而不用 .GetValues(“ElementIdName”) 确切指定哪个元素值
目前使用以下代码创建一个包含所有内容的字符串数组(元素) 来自 Request.Form.GetValues("ElementIdName") 的字符串值,问题是为了 为了使我的视图中的所有下拉列表都必须具有相同的元素 ID 名称, 我不希望他们这样做,原因很明显。所以我想知道是否有什么办法可以让我得到 Request.Form 中的所有字符串值,无需显式指定元素名称。理想情况下,我只想获取所有下拉列表值,我对 C# 不太热衷,但没有某种方法可以获取以“List”+“**”开头的所有元素 ID,所以我可以将我的列表命名为 List1 、List2、List3 等。
谢谢..
[HttpPost]
public ActionResult OrderProcessor()
{
string[] elements;
elements = Request.Form.GetValues("List");
int[] pidarray = new int[elements.Length];
//Convert all string values in elements into int and assign to pidarray
for (int x = 0; x < elements.Length; x++)
{
pidarray[x] = Convert.ToInt32(elements[x].ToString());
}
//This is the other alternative, painful way which I don't want use.
//int id1 = int.Parse(Request.Form["List1"]);
//int id2 = int.Parse(Request.Form["List2"]);
//List<int> pidlist = new List<int>();
//pidlist.Add(id1);
//pidlist.Add(id2);
var order = new Order();
foreach (var productId in pidarray)
{
var orderdetails = new OrderDetail();
orderdetails.ProductID = productId;
order.OrderDetails.Add(orderdetails);
order.OrderDate = DateTime.Now;
}
context.Orders.AddObject(order);
context.SaveChanges();
return View(order);
}
Currently using the below code to create a string array (elements) that contains all
string values from Request.Form.GetValues("ElementIdName"), the problem is that in order for
this to work all my dropdown lists in my View have to have the same element ID name which
I don't want them to for obvious reasons. So I am wondering if there's any way for me to get
all the string values from Request.Form without explicitly specifying the element name. Ideally I would want to get all dropdown list values only, I am not too hot in C# but isn't there some way to get all element ID's starting with say "List" + "**", so I could name my lists List1, List2, List3 etc.
Thanks..
[HttpPost]
public ActionResult OrderProcessor()
{
string[] elements;
elements = Request.Form.GetValues("List");
int[] pidarray = new int[elements.Length];
//Convert all string values in elements into int and assign to pidarray
for (int x = 0; x < elements.Length; x++)
{
pidarray[x] = Convert.ToInt32(elements[x].ToString());
}
//This is the other alternative, painful way which I don't want use.
//int id1 = int.Parse(Request.Form["List1"]);
//int id2 = int.Parse(Request.Form["List2"]);
//List<int> pidlist = new List<int>();
//pidlist.Add(id1);
//pidlist.Add(id2);
var order = new Order();
foreach (var productId in pidarray)
{
var orderdetails = new OrderDetail();
orderdetails.ProductID = productId;
order.OrderDetails.Add(orderdetails);
order.OrderDate = DateTime.Now;
}
context.Orders.AddObject(order);
context.SaveChanges();
return View(order);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以获取Request.Form中的所有键,然后比较并获得您想要的值。
您的方法主体将如下所示:-
You can get all keys in the Request.Form and then compare and get your desired values.
Your method body will look like this: -
Waqas Raja 的回答带有一些 LINQ lambda 的乐趣:
Waqas Raja's answer with some LINQ lambda fun:
这是一种无需向表单元素添加 ID 即可完成此操作的方法。
然后 ids 将包含选择列表中所有选定的选项值。另外,您可以像这样沿着 Model Binder 路线走:
希望这会有所帮助。
Here is a way to do it without adding an ID to the form elements.
Then ids will contain all the selected option values from the select lists. Also, you could go down the Model Binder route like so:
Hope this helps.
Request.Form 是一个NameValueCollection。在 NameValueCollection 中,您可以找到 GetAllValues() 方法。
顺便说一句,LINQ 方法也可以工作。
Request.Form is a NameValueCollection. In NameValueCollection you can find the GetAllValues() method.
By the way the LINQ method also works.