2012-09-16 20 views
1

我有值獲取表單值在剃刀asp.net陣列

  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 /> 

我想給這個數據庫

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

    } 

這種形式是不工作請幫助

+0

好嗎?任何幫助都會大大減少。謝謝Price
條件
品牌
string [] items = Request.Form.GetValues(「items」); foreach(var ax in items){Response.Write(「
」+ ax +「 - 」+ Request ['id']); } – ktm

回答

4

您應該必須使用items值爲所有input並通過Request.Form.GetValues("items")方法讀取值。

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

和讀取數值,

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

或者選擇屬性不同的值。

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

和讀取鍵值,改變名稱ITAM我能得到的值,但是有一種方式來獲得相關的ID在foreach輸出後

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

當我做字符串[] allitems = Request.Form.GetValues(「items」);我得到的錯誤對象引用未設置爲對象的實例。 – ktm

+0

在第一次請求時它將爲空,因此在讀取數組元素之前驗證引用。 – adatapost