2013-04-12 19 views
2

我有一個視圖,其中包含自動生成input類型text框。當我點擊「電子郵件結果」按鈕時,代碼會將您帶到CalculatedResults控制器中的EmailResults操作。到現在爲止還挺好。但是,即使我將FormCollection作爲EmailResults Action中的一個參數,它會以空值形式出現,我不知道爲什麼。我需要能夠從表格中捕獲文本框 - 請幫助!!在下面的View代碼中,生成的文本框稍微有點過時。試圖讓我的文本字段作爲FormCollection通過按鈕按下,但FormCollection通過爲空

我查看代碼

@using (Html.BeginForm("EmailResults", "CalculatedResults", FormMethod.Post, new { data_ajax = "false" })) 
     { 
      <table> 
       <thead> 
        <tr> 
         <td>Ingredient</td> 
         <td>Qty (gm)</td> 
        </tr> 
       </thead> 
       @foreach (var i in Model) 
       { 
        if (Convert.ToDecimal(i.Qty) < 0) 
        { 
         <tr> 
          <td style="border: 1px solid red; color: red;">@i.Ingredient</td> 
          <td style="border: 1px solid red; color: red;">@i.Qty</td> 
         </tr> 
        } 

        else if (Convert.ToDecimal(i.Qty) == 0m) 
        { 
         continue; 
        } 

        else 
        { 
         if (i.Ingredient.Contains("Active")) 
         { 
         <tr> 
          <td>@i.Ingredient<br /> 
           <input type="text" name="actives" /></td> 
          <td>@i.Qty</td> 
         </tr> 
         } 
         else 
         { 
         <tr> 
          <td>@i.Ingredient</td> 
          <td>@i.Qty</td> 
         </tr> 
         } 
        } 
       } 

      </table> 
     } 
     <div style="float: left"> 
      @Html.ActionLink("Email Results", "EmailResults", "CalculatedResults", new { crpk = @ViewBag.crpk }, new { data_icon = "arrow-r", data_role = "button" }) 
     </div> 

我的控制器代碼

public ViewResult EmailResults(int crpk, FormCollection collection) 
{ 
    CapWorxQuikCapContext context = new CapWorxQuikCapContext(); 

    //List<string> variables = new List<string>(); 
    //foreach (var item in Request.Form) 
    //{ 
    // variables.Add(item.ToString()); 
    //} 

    CalculatedResults cr = (from i in context.CalculatedResults where i.Pk == crpk select i).SingleOrDefault(); 
    Helpers.Email.EmailResults(cr); 

    return View(); 
} 

以下是截圖:

enter image description here

回答

4

formCollection只會在您將表單提交給控制器時纔會出現。無論是

  1. 使用<input type="submit" value="Email Results" />
  2. 線了一個jQuery處理程序的ActionLink的,你已經創建了一個提交表單你。
+0

是否可以通過常規發送路由值?我需要能夠發送我的@ ViewBag.crpk值。 –

+1

不是沒有javascript爲您構建URL或將FormMethod更改爲GET。您仍然需要執行上述兩個選項之一,但它會通過路由值/ URL而不是POST來完成。 – Tommy