當我提出,我沒有當它擊中我的更新操作方法
這是正常的得到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" }
))
{
...
}
非常感謝,你是對的。現在我沒有提到,最初我也有新的{id = 0,class =「someClass」},並根據您的回覆上面的提示,我現在得到0,它的工作原理,但我失去了類的格式(CSS格式)。 – PositiveGuy 2012-02-20 21:05:06
@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
今天真的很感謝你的幫助,這裏好東西!學到了很多。 – PositiveGuy 2012-02-20 23:20:25