可以在 MVC 的控制器中访问隐藏变量值吗

发布于 2024-09-06 13:18:43 字数 67 浏览 1 评论 0原文

如果隐藏变量的值是在 javascript 中设置的,我们可以访问控制器的 ActionResult 中的隐藏变量值吗?

can we access the hidden variables values in a controller's ActionResult if the hidden variable's value is set in javascript?

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

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

发布评论

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

评论(2

ま柒月 2024-09-13 13:18:43

当您将数据发布到控制器时,可以发布整个表单的内容(包括隐藏字段)。隐藏字段的目的是将其隐藏在客户端中,而不是在将数据发送到服务器时隐藏。表单数据被打包在 POST 中并发送到服务器,无论它是如何填充的,因此您可以使用 JQuery 来填充字段就好了...

给定这个表单:

<% Html.BeginForm(); %>
  <input type="hidden" id="catID" name="catID" />
<% Html.EndForm(); %>

您可以将其作为路由参数处理:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DoSomething(string catID)
{
  // Do stuff here...
}

或者,作为 FormCollection 实例的一项:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DoSomething(FormCollection form)
{
  string catID = form["catID"];

  // Do stuff here...
}

或者甚至作为输入模型:

public class MyInputModel
{
  public string catID;
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DoSomething(MyInputModel input)
{
  string catID = input.catID;

  // Do stuff here...
}

When you post data to a Controller, the entire form's contents (including hidden fields) can be posted. The purpose of a hidden field is to hide it in the client, not when it's data is sent to the server. Form data is packaged up in a POST and sent to the server, regardless of how it is populated, so you can use JQuery to populate the fields just fine...

Given this form:

<% Html.BeginForm(); %>
  <input type="hidden" id="catID" name="catID" />
<% Html.EndForm(); %>

You could process it as a route parameter:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DoSomething(string catID)
{
  // Do stuff here...
}

Or, as an item of the FormCollection instance:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DoSomething(FormCollection form)
{
  string catID = form["catID"];

  // Do stuff here...
}

Or even as an input model:

public class MyInputModel
{
  public string catID;
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DoSomething(MyInputModel input)
{
  string catID = input.catID;

  // Do stuff here...
}
如梦亦如幻 2024-09-13 13:18:43

如果您指的是 ASP.NET MVC,并且您提到的隐藏变量实际上是发布到控制器的表单中的隐藏字段,那么答案是肯定的。

这是我编写的应用程序中的常见模式。例如,假设您正在编辑一个人的详细信息。您填写的表单将包含姓名、年龄等可见字段,但还需要一个隐藏字段,用于保存您正在为其编辑详细信息的人员的 ID。

如果这是您正在使用的场景,那么控制器可以使用隐藏字段,其方式与名称和字段相同。年龄领域。

编辑:根据您随后的评论,看起来您指的是 javascript 变量。如果是这种情况,那么控制器无法直接使用它们 - 但可以通过将变量插入到表单中来进行安排。

//Javaacript
var myVariable = calculateSomeValue();
$("#myFormField").val(myVariable);
...
//HTML
<form action="..." method="post">
    <input type="hidden" name="myFormField" id="myFormField"/>
    ...
</form>
...
//Controller code
ActionResult MyControllerAction(string myFormField, ...){
    DoSomethingWith(myFormField);
}

如果这没有帮助,您可以发布一些示例代码吗?

If you are referring to ASP.NET MVC, and the hidden variable that you mention is actually a hidden field in a form that is being posted to the controller, then the answer is yes.

This is a frequent pattern in the apps that I write. Say, for example, that you are editing the details of a person. The form that you fill in would contain visible fields for things like name, age, etc - but would also need to have a hidden filed that holds the ID of the person that you are editing the details for.

If this is the kind of scenario that you are using, then the hidden field is available to the controller in the same way as the name & age fields.

EDIT: Further to your subsequent comment, it looks like you are referring to javascript variables. If this is the case, then these are not available to the controller directly - but it is possible to arrange this by inserting the variable(s) into the form.

//Javaacript
var myVariable = calculateSomeValue();
$("#myFormField").val(myVariable);
...
//HTML
<form action="..." method="post">
    <input type="hidden" name="myFormField" id="myFormField"/>
    ...
</form>
...
//Controller code
ActionResult MyControllerAction(string myFormField, ...){
    DoSomethingWith(myFormField);
}

If this doesn't help, can you post some example code?

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