2012-02-20 83 views
0

在我的.cshtml中,我正在繪製一些數據。然後我有一個repy文本框和一個按鈕供人們回覆客戶服務線程。路徑值保持不變

@using (Html.BeginForm("Update", "CustomerServiceMessage", FormMethod.Post, new { id = 0 })) 
    ... 
} 

當我提出,我沒有得到0,當它擊中我的更新actionmethod,我得到我的答覆框上方繪父服務消息的ID。所以它就像一個電子郵件/論壇主題,但即使我對= 0進行硬編碼,Update方法也會獲得我在屏幕上呈現的父消息的Id(呈現)。

找不到原因。

回答

5

當我提出,我沒有當它擊中我的更新操作方法

這是正常的得到0,你從不發送此ID到你的服務器。你剛纔所用的Html.BeginForm幫手wrong overload

@using (Html.BeginForm(
    "Update",      // actionName 
    "CustomerServiceMessage",  // controllerName 
    FormMethod.Post,     // method 
    new { id = 0 }     // htmlAttributes 
)) 
{ 
    ...  
} 

和你結束了以下標記(假定的缺省路由):

<form id="0" method="post" action="/CustomerServiceMessage/Update"> 
    ... 
</form> 

看這個問題?

而這裏的correct overload

@using (Html.BeginForm(
    "Update",      // actionName 
    "CustomerServiceMessage",  // controllerName 
    new { id = 0 },     // routeValues 
    FormMethod.Post,     // method 
    new { @class = "foo" }   // htmlAttributes 
)) 
{ 
    ...  
} 

產生(假設默認路由):

<form method="post" action="/CustomerServiceMessage/Update/0"> 
    ... 
</form> 

現在,你會得到你id=0內部相應的控制器動作。

順便說一句,你可以使你的代碼更具可讀性和使用C# 4.0 named parameters避免這種錯誤的:

@using (Html.BeginForm(
    actionName: "Update", 
    controllerName: "CustomerServiceMessage", 
    routeValues: new { id = 0 }, 
    method: FormMethod.Post, 
    htmlAttributes: new { @class = "foo" } 
)) 
{ 
    ...  
} 
+0

非常感謝,你是對的。現在我沒有提到,最初我也有新的{id = 0,class =「someClass」},並根據您的回覆上面的提示,我現在得到0,它的工作原理,但我失去了類的格式(CSS格式)。 – PositiveGuy 2012-02-20 21:05:06

+1

@CoffeeAddict,然後只需使用[正確的重載](http://msdn.microsoft.com/en-us/library/dd460542.aspx):@using(Html.BeginForm(「Update」,「CustomerServiceMessage」, new {id = 0},FormMethod.Post,new {@class =「foo」})){...}。你沒有閱讀過文檔嗎?或者看一下Visual Studio中的Intellisense,它告訴你正在調用的方法的參數? – 2012-02-20 21:08:59

+0

今天真的很感謝你的幫助,這裏好東西!學到了很多。 – PositiveGuy 2012-02-20 23:20:25