2012-02-20 71 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