如何从 Request.Form 获取所有元素值,而不用 .GetValues(“ElementIdName”) 确切指定哪个元素值

发布于 2024-11-10 04:56:39 字数 1340 浏览 4 评论 0原文

目前使用以下代码创建一个包含所有内容的字符串数组(元素) 来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爺獨霸怡葒院 2024-11-17 04:56:39

您可以获取Request.Form中的所有键,然后比较并获得您想要的值。

您的方法主体将如下所示:-

List<int> listValues = new List<int>();
foreach (string key in Request.Form.AllKeys)
{
    if (key.StartsWith("List"))
    {
        listValues.Add(Convert.ToInt32(Request.Form[key]));
    }
}

You can get all keys in the Request.Form and then compare and get your desired values.

Your method body will look like this: -

List<int> listValues = new List<int>();
foreach (string key in Request.Form.AllKeys)
{
    if (key.StartsWith("List"))
    {
        listValues.Add(Convert.ToInt32(Request.Form[key]));
    }
}
遇到 2024-11-17 04:56:39

Waqas Raja 的回答带有一些 LINQ lambda 的乐趣:

List<int> listValues = new List<int>();
Request.Form.AllKeys
    .Where(n => n.StartsWith("List"))
    .ToList()
    .ForEach(x => listValues.Add(int.Parse(Request.Form[x])));

Waqas Raja's answer with some LINQ lambda fun:

List<int> listValues = new List<int>();
Request.Form.AllKeys
    .Where(n => n.StartsWith("List"))
    .ToList()
    .ForEach(x => listValues.Add(int.Parse(Request.Form[x])));
望她远 2024-11-17 04:56:39

这是一种无需向表单元素添加 ID 即可完成此操作的方法。

<form method="post">
    ...
    <select name="List">
        <option value="1">Test1</option>
        <option value="2">Test2</option>
    </select>
    <select name="List">
        <option value="3">Test3</option>
        <option value="4">Test4</option>
    </select>
    ...
</form>

public ActionResult OrderProcessor()
{
    string[] ids = Request.Form.GetValues("List");
}

然后 ids 将包含选择列表中所有选定的选项值。另外,您可以像这样沿着 Model Binder 路线走:

public class OrderModel
{
    public string[] List { get; set; }
}

public ActionResult OrderProcessor(OrderModel model)
{
    string[] ids = model.List;
}

希望这会有所帮助。

Here is a way to do it without adding an ID to the form elements.

<form method="post">
    ...
    <select name="List">
        <option value="1">Test1</option>
        <option value="2">Test2</option>
    </select>
    <select name="List">
        <option value="3">Test3</option>
        <option value="4">Test4</option>
    </select>
    ...
</form>

public ActionResult OrderProcessor()
{
    string[] ids = Request.Form.GetValues("List");
}

Then ids will contain all the selected option values from the select lists. Also, you could go down the Model Binder route like so:

public class OrderModel
{
    public string[] List { get; set; }
}

public ActionResult OrderProcessor(OrderModel model)
{
    string[] ids = model.List;
}

Hope this helps.

初熏 2024-11-17 04:56:39

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文