0

Quite new to MVC so please bear with me. I'm trying to load a partial view in a modal from a different controller and everytime I try to view it I get just an empty modal. I believe it is because I haven't been able to instantiate my model in the view.

E.g. I have a Controller 'Home' with a method 'Details' that returns a partialview from a different View folder. The native model to the controller is 'model', whereas the model belonging to my other controller is 'model2'.

public ActionResult Details() {
    model2.User = user;        //this is a global variable
    model2.GetDetails();

    return PartialView("~/Views/...Details", model2);
}

I'm sure the reason is because i'm missing the model data in the view. I tried adding another @model... to the view but clearly this doesn't work.

Is there a way of doing what I am trying to accomplish? It can even be a relatively dirty solution as this is a stopgap solution for the time being.

Reading back over this post it reads a little convoluted so if any clarification is needed please let me know.

Thanks

bjjrolls
  • 529
  • 4
  • 8
  • 21
  • How are you trying to show the partial? (you need to use `@Html.Action()`) –  Jul 11 '16 at 09:47
  • Hey, I don't think it's a problem with that as the view hasn't changed. It was working with another controller (its native controller) but I have had to change it temporarily to using another controller. I'm calling the controller using ajax, and on the success populating the div like $('#partial').html(data); – bjjrolls Jul 11 '16 at 09:59
  • Then show your relevant code and indicate what your problem is (and its just `return PartialView(model2);`) –  Jul 11 '16 at 10:01
  • 1
    For dynamic partial view with different controllers, using a viewmodel to hold multiple models may be reasonable (combined with solution from http://stackoverflow.com/questions/7968234/render-partial-view-from-other-controller). – Tetsuya Yamamoto Jul 11 '16 at 10:02

1 Answers1

1

I faced this problem once before and i think it's a lot of work to reproduce it to provide an exact solution, but I can offer my 2 cents. The thing with browser Modals is that you need to provide a url when you are opening it. The URL will have to be the Controller/Action url and this is the tricky part which causes the problem. If you can figure that out, you should be able to solve the problem. If you can't, you can do one of the following: 1. Set the HTML content of Modal dialog from your main window's JS code after the Modal is opened. 2. Use one of the 3rd party HTML/CSS modal implementation, and set the HTML content from the JS code. In this case there is no browser modal and everything is on the same page.

To verify if the view is returning correctly from XHR, put the actionRoute URL in the browser address bar and you will see the content getting returned. It will help with troubleshooting.

Saharsh
  • 750
  • 7
  • 18