skip to Main Content

We have got two controllers:
HomeController
ExampleController

HomeController has a view, lets say Index, which as a model has Home model.
ExampleController has a method, which takes id (obtained from Home model) as a parameter, and returns a partial view _examplePartial with a model of a list Example models.

In a Index view of HomeController, i need to show this partial, but it need to be filled with data from ExampleController.
Is it possible to do that?

@Html.RenderAction – doesn’t work
@Html.Partial – of course also doesn’t work

2

Answers


  1. In your ExampleController, you would need to modify the ExampleAction method to accept the "id" parameter and use it to populate the list of Example models that will be passed to the partial view. The partial view can then be returned using the PartialView method:

    public ActionResult ExampleAction(int id)
    {
        // Retrieve Example models based on the id parameter
        List<Example> examples = RetrieveExamples(id);
    
        // Pass the examples list to the partial view
        return PartialView("_examplePartial", examples);
    }
    

    With these changes, the Html.Action method should now render the _examplePartial view with the list of Example models obtained from ExampleController.

    Login or Signup to reply.
  2. @Html.RenderAction would work for your requirement in asp.net

    It seems you are trying with a .net core MVC app, you’d better modify the tag(remove asp.net related tag)
    I tried with two solutions for your requirement in default asp.net core app:

    Load partial view with jquery codes:

    Controllers:

    public IActionResult Index(int i)
            {
                ViewData["partial"] = i;
                return View();
            }
            public IActionResult Partial(int id)
            {
                var vm = new List<SomeModel>();
                for(int i=0; i < id; i++)
                {
                    vm.Add(new SomeModel() { Id = 1, Prop1 = "prop" + i.ToString() });
                }
    
                return PartialView("Partial",vm);
            }
    

    View:

    Index

    @{
        ViewData["Title"] = "Home Page";
        var uri = "/Home/Partial/" + ViewData["Partial"].ToString();
    }
    
    <div class="text-center">
        <h1 class="display-4">Welcome</h1>
        <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
    </div>
    
    <div id="somediv">
    
    </div>
    
    
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script>
        $("#somediv").load("@uri")
    </script>
    

    Partial:

    @model IEnumerable<SomeModel>
    
    
    <table>
        <tr>
            <th>Id</th>
            <th>Prop</th>
        </tr>
    
    
    
        @foreach (var item in Model)
        {
            <tr>
                <td>@item.Id</td>
    
                <td>@item.Prop1</td>
            </tr>
        }
    </table>
    

    ViewComponent Solution:

    public class SomeViewComponent : ViewComponent
        {
            public async Task<IViewComponentResult> InvokeAsync(int id)
            {
                var vm = await FetchEntity(id);
                return View("Partial",vm);
            }
    
            private async Task<List<SomeModel>> FetchEntity(int id)
            {
                var vm = new List<SomeModel>();
                for (int i = 0; i < id; i++)
                {
                    vm.Add(new SomeModel() { Id = 1, Prop1 = "prop" + i.ToString() });
                }
                return await Task.FromResult(vm);
            }
        }
    

    In privacy page called:

    @await Component.InvokeAsync("Some", 2)
    

    Structure:

    enter image description here

    Result:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search