简体   繁体   中英

Load partial view with parameters

I'm trying to load a partial view with Id parameters inside "Bootstrap Model"

I can't pass the parameters to the controller

Something like this:

<td>
    <button class="btnShowModal btn btn-primary btnWhiteSpace " type="button">
        Details
        <i class="fas fa-info-circle" aria-hidden="true"></i>
    </button>
</td>

<div class="modal fade" tabindex="-1" id="loginModal"
     data-keyboard="false" data-backdrop="static">
    <div class="modal-dialog modal-sm ">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <i class="fas fa-window-close"></i>
                </button>
                <h4 class="modal-title"> Title</h4>
            </div>
            <div class="modal-body ">
                <partial name=("bid", "details" asp-route-id="item.QuettaReqId" ) />
            </div>
        </div>
    </div>
</div>

The error I'm getting is:

InvalidOperationException: The partial view '(' was not found. The following locations were searched:
Views/Mail/(.cshtml
/Views/Shared/(.cshtml
/Pages/Shared/(.cshtml

It look like routing but I think that the parameter is set also not being pass as should.

Try this:

@{ Html.RenderAction("YourActionName", "ControllerName", new { parameterName = parameterValue });}

So in case of you as follows:

<div class="modal-body ">
  @{ Html.RenderAction("details", "bid", new { id= item.QuettaReqId});}
</div>

Now if you want a ASP.NET Core specific solution then you can use View Components which is much more faster than Partial View.

For passing item.QuettaReqId from Main view to partial view, you need to specify the right Partial View Name and model.

<div>
     <partial name="_Details" model="10"/>
     @await Html.PartialAsync("_Details", 5)
</div>

For _Details , you need to define @model to accept the parameter.

@model int

This is Detail View with Id @Model

Note , _Details is the Partial View Name.

For more information about model , check model

The error you are getting because asp.net engine is unable to find the view

You ma replace it with

@Url.Action("YourActionMethod", "Controller", new { param = "1", param2= "2" })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM