0

I have an application in ASP.MVC. The requirement is that I select a person from a list of people and click 'Info' and it should load the details of the person in that page. I have the Info controller and everything works fine if I go to the Info page from a different controller. In the page I am trying to make it work with JavaScript and it doesn't seem to take me to the desired page but to a different controller.

I have a ToDoList controller and in the .cshtml I have this code on click of the Info link.

 function DoInfo@(i.ToString())() {
         $("#sessionid").val("@Model.cSessionId[i]");
         alert("hey");
         $("#PageController").val(66);
         $("#formID").submit();
    }

I go to the ToDoList controller to do the redirection like this

  if (viewModel.PageController == 66)  
        {
            pass = new PassingData();
            pass.personid = TSSessionService.ReadPersonId(viewModel.SessionId);
            TempData["pass"] = pass;

            return RedirectToAction("Index", "Info");
        }

It never goes there and instead goes to a different controller. I cannot seem to find how they are linked and why is it not going back to controller where the Info link button is i.e. back to the ToDoList controller.

Let me know if it is not clear and I will try to explain again and I will give any other details.

Mahmoud Gamal
  • 72,639
  • 16
  • 129
  • 156
Tulips
  • 809
  • 5
  • 12
  • 21
  • Are there areas in your MVC application and two different areas having the same controller name? – Siva Gopal Jul 10 '12 at 14:40
  • There is Info controller that needs to be called from different areas. It works fine when I call it from 'AP' controller but not when I try to call from 'ToDoList' controller. I do get the alert but not sure why the control goes to 'Double' controller. – Tulips Jul 10 '12 at 14:46
  • Did you try debugging the C# code block by keeping a breakpoint to make sure some other codeblock inside the action method is getting executed and redirecting to other controller? Also, check if proper route constraint defined for the Area & Controller combination otherwise generic/default constraint will get priority. – Siva Gopal Jul 10 '12 at 14:49
  • How do I pass a value from a hidden field to the controller through the javascript that I have? i need to pass the selected id to the controller through the javascript. – Tulips Jul 10 '12 at 15:07
  • I tried this but no luck function DoInfo@(i.ToString())() { $("#SessionId").val($("#sessionid@(i.ToString())").val()); alert("hey"); $("#PageController").val(72); $("#formID").submit(); } – Tulips Jul 10 '12 at 15:16
  • So, from above two messages, did you found that your selected value is not posting to controller after debugging? – Siva Gopal Jul 11 '12 at 12:18
  • Yes, that's what I found out. Selected value doesn't reach my controller. Not sure if the way I do is the correct way? – Tulips Jul 12 '12 at 07:44

1 Answers1

0

I guess I'm confused as to why you are doing this as a combination of form and JavaScript. Are there other properties that you need to pass along that you are not posting above? Why do you need to use JavaScript to do this if you are just returning a new view?

You indicate in your post that when a person is selected from a list you need to go to a controller and display a view. This seems fairly straightforward, and I would like to suggest simplifying the problem.

Start with this: change your link to not use a form or JavaScript. Just make it a link. If it is text, you can use @Html.ActionLink() and even pass in the parameters you need.

If you're not displaying text, just use @Url.ActionLink() in your href property of the anchor you're wrapping your element with. Both of these allow you to leverage routing to ensure the correct path is being constructed.

If the controller that you are trying to get to has access to whatever TSSessionService is, then you don't need to pass through the TempData["pass"] you are trying to push through, so it makes it cleaner in that way as well.

If you do need to submit a more complicated value set, I would recommend coming up with a generic .click() event handler in jQuery that can respond to any of the clicks, bound by a common class name. You can use a data-val attribute in your link and read from $(this).attr('data-val') in your handler to store/fetch other important info. This allows you to more easily build up an object to POST to a controller.

Hope this helps some, but if I'm missing a critical point then please update the question above.

MisterJames
  • 3,226
  • 1
  • 27
  • 47
  • I can try to explain again. I have a page that displays a list of 'sessions'. Each session has three options listed. Info, Open and Mail. Now, when Info is clicked I need to load another view from a different controller that is expecting a PId to come through passing data. This is what is existing code and I need to make the Info link work. I do not have to use JavaScript but that seemed to be way as then in the controller I can read PId from the SessionId. Problem is SessionId is not getting passed through the script. I am open to using @Url.ActinLink but how do I pass PId in it? – Tulips Jul 12 '12 at 07:57
  • Right, okay, so you just need the overload for Url.Actionlink. Check out this answer: http://stackoverflow.com/questions/6278694/url-action-parameters. Cheers. – MisterJames Jul 12 '12 at 15:09