2011-01-07 78 views
0

我已經是這樣產生的一個按鈕誰的代碼:ASP.NET MVC2郵政按鈕有正確的價值觀,但不發送值行動

<% RouteValueDictionary dictionary2 = new RouteValueDictionary(); %> 
     <% dictionary2.Add("EventID",0); %> 
     <% dictionary2.Add("CustomerID",Model.customer.CustomerID.ToString()); %> 
     <% using (Html.BeginForm("EventEdit", "Customers", dictionary2,FormMethod.Get,null)) 
        { %> 
     <button type="submit"> 
      new event</button> 
     <%} %> 

實際的代碼生成:

<form action="/Customers/EventEdit?EventID=0&amp;CustomerID=1" method="get"> 

       <button type="submit"> 
        new event</button> 
       </form> 

但按鍵調用該地址:

http://localhost:20588/Customers/EventEdit

,我也得到:

參數字典包含參數 無效項「System.Int32」的 法「的 非可空類型「事件ID」 System.Web.Mvc.ActionResult EventEdit (Int32,Int32)' 'TechRun.UI.Controllers.CustomersController'。 可選參數必須是 參考類型,可爲空的類型,或者將其聲明爲可選參數 。 參數名稱:參數

任何想法我做錯了什麼(該窗體上有其他後按鈕,但他們工作正常)。 謝謝。

+0

使用調試器查看存在的值。 – leppie 2011-01-07 11:06:12

+0

我檢查頁面的源代碼,也使用螢火蟲 - 我得到正確的代碼:/ Customers/EventEdit?EventID = 0 & CustomerID = 1。我已經拿到了這些值並將它們粘貼到地址欄上 - 並且它工作正常。 – Dani 2011-01-07 11:11:15

回答

1

嘗試 like this

<% using (Html.BeginForm(
    "EventEdit", 
    "Customers", 
    new { EventId = "0", CustomerID = Model.customer.CustomerID }, 
    FormMethod.Get)) 
{ %> 
    <input type="submit" value="new event" /> 
<% } %> 

還要確保有沒有一些JavaScript可與表單提交Ajax請求,並忘記包括價值觀干擾。最後確保 Model.customer.CustomerID不是空的。使用FireBug精確地查看發送到服務器的請求。


UPDATE:

按照specification

如果該方法是 「獲取」 和動作 是HTTP URI,用戶代理採取 動作的值,附加一個'?'到 它,然後附加表單數據集 編碼使用 「application/x-www-form-urlencoded」 內容類型。

這意味着您不應該在GET方法的表單action中使用查詢字符串參數。您需要使用表單中的隱藏字段將這些值傳遞給服務器。