简体   繁体   English

如何在同一控制器方法/视图中多次使用隐藏值?

[英]How do I use a hidden value more than once in the same controller method/view?

I have a method that list listing directories and you can drill in the directories. 我有一个列出目录的方法,您可以在目录中进行钻取。 The method looks like this: 该方法如下所示:

           public ActionResult ListObjects(string Prefix)
           {
             if(string.isnullorempty(Prefix))
                //Present root files and directories
             else
                //Present directory choosed with Prefix
           }

On the view ListObjects, I am experiencing the Prefix hidden field not changing value after the first time it has a value. 在视图ListObjects,我遇到的前缀隐藏字段的第一次有一个值后不改变价值。 I proofed that by adding and extra field that does changes the value after the first time it has one. 我通过添加一个额外的字段来证明这一点,它确实在第一次拥有一个值之后会改变它的值。 for example: the first time you process the listobjects method prefix is null, and item.prefix has the first value for each directory, but after you click on any directory in the first view, the second time the controller is called the actual value never changes. 例如:第一次处理listobjects方法的前缀为null,而item.prefix具有每个目录的第一个值,但是在第一个视图中单击任何目录后,第二次将控制器称为实际值,从不变化。

                <%= Html.Hidden("Prefix",item.Prefix) %>
                <%= Html.Hidden("TestVariable" ,item.Prefix) %>

This is a little proof of what is actually happening. 这是实际情况的一点证明。

   <input id="Prefix" type="hidden" value="CP/" name="Prefix"/>
   <input id="TestVariable" type="hidden" value="CP/CPTest/" name="TestVariable"/>

My objective is to have the input id="Prefix" to change in every call, and not stay static after the first time it gets a value. 我的目标是让输入id =“ Prefix”在每次调用中进行更改,并且在第一次获取值后不保持静态。 As you can see the two input fields above the Prefix has CP while the testvariable has cp/cptest which is the value that I wanted, but both input fields are being taken from the same variable. 如您所见,Prefix上方的两个输入字段具有CP,而testvariable具有cp / cptest,这是我想要的值,但是两个输入字段都取自同一变量。

EDIT 2: 编辑2:

I think it has to do with the fact that strings are references and since mvc framework sees that Prefix has a value from a previous request it overrides the new value assignment from model.Prefix. 我认为这与字符串是引用这一事实有关,并且由于mvc框架看到Prefix具有来自先前请求的值,因此它会覆盖来自model.Prefix的新值分配。


NOTE: I posted the question yesterday, and I answer the question myself after a little troubleshooting. 注意:我昨天发布了问题,经过一点故障排除后我自己回答了问题。 The solution that I found is not the ideal, but is working and I am done unless someone here is able to give me a better way of achieving the same. 我找到的解决方案是不理想的,但工作,我做了,除非有人在这里是能够给我达到同样的更好的方法。 Please let me know. 请告诉我。 Geo 地缘

Maybe because the second time you're processing the listobjects your flag is not null nor empty. 可能是因为第二次处理列表对象时,标志既不为null也不为空。 You're changing the state of the object in the first call. 您将在第一个调用中更改对象的状态。

I am sure this is not the ideal solution, but I got around my issue by not using the MVC helper files. 我确定这不是理想的解决方案,但是通过不使用MVC帮助程序文件解决了我的问题。 Instead of using the Html.Hidden helper I used directly the input field as below: 我没有使用Html.Hidden帮助器,而是直接使用了输入字段,如下所示:

  <%--<%= Html.Hidden("Prefix",item.Prefix) %>--%>                
  <input id="Prefix" type="hidden" value="<%= item.Prefix %>" name="Prefix" />  

This works like a charm, if you think of a reason why the html.Hidden is not working please let me know. 这就像一种魅力,如果您想到html.Hidden无法正常工作的原因,请告诉我。

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

相关问题 我如何使用Nhibernate多次在同一张表上进行内部联接 - How do I inner join on the same table more than once using Nhibernate 如何防止部分视图中的脚本多次加载并在同一页面中多次使用部分时导致错误 - How to keep script in partial view from loading more than once and causing errors when partial is used more than once in the same page 如何限制一次多次触发事件 - How do I restrict events firing more than once at a time 如何在linq中多次使用var? - How to use var more than once in linq? C#-如何多次使用相同的对象使用不同的值? - C# - How to use the same object more than once with different values? 如何在 controller 中使用多个 DBContext - How to use more than one DBContext in a controller 我可以让相同的元素在列表中重复多次吗? - Can I have the same element repeat more than once in a list? 如何在 5 秒内停止多次调用方法? - How can I stop a method being called more than once in 5 seconds? 如何为多个目标对象使用故事板? - How do I use a storyboard for more than 1 target object? 如何在多个表格中检查ID值 - How do I check for an ID value in more than one table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM