views:

100

answers:

2

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. 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.

                <%= 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. 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.

EDIT 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.


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

A: 

Maybe because the second time you're processing the listobjects your flag is not null nor empty. You're changing the state of the object in the first call.

Juparave
A: 

I am sure this is not the ideal solution, but I got around my issue by not using the MVC helper files. Instead of using the Html.Hidden helper I used directly the input field as below:

  <%--<%= 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.

Geo