MVC2:如何读取操作链接的控制值?

发布于 2024-08-17 03:05:38 字数 386 浏览 7 评论 0原文

我将一些模型信息传递给 ActionLink,但我还想为操作提供页面上某些输入的值。例如,如果我有这样的事情:

<input Name="MyInput" />

<%: Html.ActionLink("MyAction", "MyController", Model.Value);

我希望操作能够了解 Model.Value(通过参数传入)和 MyInput 的值。通常我会使用 FormCollection,但在这种情况下我不能使用,因为我没有进行提交。

那么如何将 MyInput 的值传递给 MyAction 呢?我是否必须将名为 MyInput 的属性添加到我的模型中?假设这可行,是否有一种更简单的方法,或者至少有一种不涉及修改模型的方法?

I'm passing in some model information to an ActionLink, but I'd also like to provide the action with the values of some inputs on the page. For example, if I had something like this:

<input Name="MyInput" />

<%: Html.ActionLink("MyAction", "MyController", Model.Value);

I'd like the action to be aware of both Model.Value (which is passed in via parameter), and the value of MyInput. Normally I would use a FormCollection, but I can't in this instance since I'm not doing a submit.

So how can I pass the value of MyInput to MyAction? Would I have to add a property named MyInput to my model? Assuming that would work, is there an easier way, or at least one that doesn't involve modifying the model?

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

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

发布评论

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

评论(2

给妤﹃绝世温柔 2024-08-24 03:05:38

在 HTML 中无法做到这一点,因此在 ASP.NET MVC 中也无法做到这一点。

您可以选择两种可能的解决方案:

  1. 使用 JavaScript,这样当用户编辑文本框时,您可以动态更改锚标记的值以包含他们输入的内容。您不能使用 ASP.NET为此进行路由,因为它在服务器上运行,并且您需要客户端代码。

  2. 提交表单而不是链接。这是 HTML 中推荐的方式。当用户提交数据时,应该以表单的形式提交。将所有内容包装在表单标签中,并将文本框和按钮放在其中。将表单的操作设置为您希望其发布到的 URL。

这与我在这个问题中的回答相同,尽管提问者另一个问题可能有稍微不同的想法。

There is no way to do this in HTML, thus there isn't a way to do this in ASP.NET MVC.

There are two possible solutions to this that you can choose from:

  1. Use JavaScript such that when the user edits the textbox you dynamically change the value of the anchor tag to include what they typed in. You can't use ASP.NET routing for this because that runs on the server and you need client side code.

  2. Do a form submit instead of a link. This is the recommended way in HTML. When the user is submitting data, it should be in a form. Wrap everything in a form tag and place the textbox and a button in there. Set the form's action to be the URL that you want it to post to.

This is the same response as I put in this question, though the asker of the other question might have had a slightly different idea.

奶茶白久 2024-08-24 03:05:38

我确定我可能可以通过 Javascript 读取文本框的值(我可能会使用 JQuery),并将它们作为参数作为匿名类型的一部分传递(而不是仅使用 Model.Value)。但是,我决定从使用操作链接切换到仅执行表单提交并使用 FormCollection。这对于我正在构建的页面来说更有意义。

I determined that I probably could have read the values of the text box via Javascript (I probably would have used JQuery), and passed them as parameters as part of an anonymous type (instead of just using Model.Value). However, I decided to switch from using an action link to just doing a form submit and using the FormCollection. It just made more sense with the page I was building.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文