简体   繁体   English

将变量从视图传递到 controller 而不使用查询字符串

[英]Passing variable from view to the controller without query string

I'm fairly sure I'm missing something very practical but I'm floundering at finding it online.我很确定我错过了一些非常实用的东西,但我在网上找到它时很挣扎。

How do you pass information from the view to the controller without using URL items (query strings, /variable/ etc?如何在不使用 URL 项目(查询字符串、/变量/等)的情况下将信息从视图传递到 controller?

The easiest scenario I can think that surely must have an example is a simple set of login credentials handed to a controller.我认为肯定必须有一个示例的最简单的场景是一组简单的登录凭据传递给 controller。 Naturally you wouldn't want the username / password to be viewable in the URL.当然,您不希望用户名/密码在 URL 中可见。

You can POST data to the controller, so that it doesn't appear in the URL.您可以将数据 POST 到 controller,这样它就不会出现在 URL 中。 See this blog post for details.有关详细信息,请参阅此博客文章

The basic steps are:基本步骤是:

  1. Create a form on the view and name the fields in which you are interested.在视图上创建一个表单并命名您感兴趣的字段。
  2. Create an action on the controller whose parameters match those names.在参数与这些名称匹配的 controller 上创建一个操作。
  3. Add the [AcceptVerbs(HttpVerbs.Post)] attribute to the action.[AcceptVerbs(HttpVerbs.Post)]属性添加到操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM