How can I get all element values from Request.Form without specifying exactly which one with .GetValues(“ElementIdName”)

微笑、不失礼 提交于 2019-11-30 12:53:13

问题


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);

回答1:


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]));
    }
}



回答2:


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])));



回答3:


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.




回答4:


Request.Form is a NameValueCollection. In NameValueCollection you can find the GetAllValues() method.

By the way the LINQ method also works.



来源:https://stackoverflow.com/questions/6159103/how-can-i-get-all-element-values-from-request-form-without-specifying-exactly-wh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!