路线价值保持不变

发布于 2025-01-07 06:13:31 字数 328 浏览 1 评论 0原文

在我的 .cshtml 中,我正在绘制一些数据。然后我有一个回复文本框和一个按钮,供人们回复客户服务线程。

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

当我提交时,当它点击我的更新操作方法时,我没有得到 0,我得到了我在回复框上方绘制的父服务消息的 id。所以它就像一个电子邮件/论坛线程,但即使我硬编码 = 0,Update 方法也会获取我在屏幕上绘制(渲染)的父消息的 ID。

不明白为什么。

In my .cshtml, I'm painting some data. Then I have a repy textbox and a button for people to reply to a customer service thread.

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

When I submit, I don't get 0 when it hits my Update actionmethod, I get the id of the parent Service message I painted above the reply box. So it's like an email/forum thread but even though I hard code the = 0 the Update method is getting an Id of the parent message that I painted on the screen (rendered).

Can't figure out why.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝眼睛不忧郁 2025-01-14 06:13:31

当我提交时,当它点击我的更新操作方法时,我没有得到 0

这是正常的,你永远不会将此 id 发送到你的服务器。您刚刚使用了 Html.BeginForm< 的错误的重载 /code> helper:

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

最终得到以下标记(假设默认路由):

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

看到问题了吗?

这是正确的重载

@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 命名参数

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

When I submit, I don't get 0 when it hits my Update action method

That's normal, you never send this id to your server. You just used the wrong overload of the Html.BeginForm helper:

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

and you ended up with the following markup (assuming default routes):

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

See the problem?

And here's the correct overload:

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

which generates (assuming default routes):

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

Now, you will get your id=0 inside the corresponding controller action.

By the way you could make your code more readable and avoid this kind of mistakes by using C# 4.0 named parameters:

@using (Html.BeginForm(
    actionName: "Update", 
    controllerName: "CustomerServiceMessage", 
    routeValues: new { id = 0 }, 
    method: FormMethod.Post,
    htmlAttributes: new { @class = "foo" }
))
{
    ...    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文