How can I get all element values from Request.Form

2019-02-02 06:49发布

问题:

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.