Getting the form values as an array in razor asp.n

2019-07-23 09:56发布

问题:

I have a form with values

           Price  <input type="text" name="items[price]" value="" /> <br />
       Condition  <input type="text" name="items[condition]" value="" /> <br />
           Brand  <input type="text" name="items[brand]" value="" /> <br />

I want to send this to database

    string[] allitems = Request.Form["items"];
    var db2x = Database.Open("mystring");

    foreach (var item in allitems)
    {
       var insertCommand2x = "INSERT INTO Classifieds_attr_value (AttrId,CarBikeId,AttrVal) VALUES(@0,@1,@2)";
       db2x.Execute(insertCommand2x, AttrId, CarBikeId, AttrVal);

   }

This is not working pls help

回答1:

You should have to use items value for all input and read values via Request.Form.GetValues("items") method.

Price      <input type="text" name="items" value="" /> <br />
Condition  <input type="text" name="items" value="" /> <br />
Brand      <input type="text" name="items" value="" /> <br />

and read values,

string []items=Request.Form.GetValues("items");
if(items!=null){
   //read
}

Or choose different value for name attributes.

Price      <input type="text" name="price" value="" /> <br />
Condition  <input type="text" name="condition" value="" /> <br />
Brand      <input type="text" name="brand" value="" /> <br />

and read key-value,

string[] keys = Request.Form.AllKeys;
if (keys!= null)
 {
  foreach (var key in keys)
   {
   Response.Write("<br/>" + key + " " + Request.Form[key]);
   }
 }


标签: asp.net razor