MVC Partial view with controller, ajax - how do I ge the partial controller to get data?

前端 未结 2 1342
闹比i
闹比i 2021-01-25 11:45

I\'m learning MVC and am stumped by this. I\'m trying to factor some common code that gets data and displays it in a WebGrid into a partial view that i can use on multiple pages

相关标签:
2条回答
  • 2021-01-25 12:20

    The partial page won't pass through a controller, but simply render the view directly. If you want to pass view data to the partial view, there is an overloaded function that takes a viewdata dictionary. I'm sorry I can't be more detailed, but I'm on my mobile (waiting for my son to fall asleep in the other room) :)

    Update:

    If you want to trigger a GET action for your partial view, you can use Html.Action. Here are some useful links:

    • MSDN RenderAction
    • Difference between RenderPartial and RenderAction

    Further, it would probably make sense for you to move your form tags into your partial view, but those are details for when you clean up the code.

    0 讨论(0)
  • 2021-01-25 12:31

    Jonass is right, the ViewBag only propagates between the Controller and the View.

    One thing you can do is make the model of the partial view be the same as the type of data you're putting into the ViewBag.

    So if for example your MyQueryResults is of type:

    IEnumerable<Result>
    

    In your partial view you'd add

    @Model IEnumerable<Result>
    

    And then in the main view, you'd pass it through the Render method:

    @Html.Partial("SearchResults", ViewBag.Members);
    

    You'll need to tweak this a bit to make sure it's the right type, but this should do the trick.

    Good luck!

    0 讨论(0)
提交回复
热议问题