0

I have a partial view that is called _GetForfaitDashBoard.cshtml This is the code

@model WebAppComp.Areas.Admin.Models.StatsForfaitDashBoard


<table class ="table table-bordered">
<tr>
<th>Nombre total de Forfaits</th>
<th>Moyenne des forfaits par Agence</th>
<th>Forfait Ayant le prix le plus haut</th>
<th>Forfait Ayant le prix le plus bas</th>
<th>Le forfait le plus visité</th>
<th>Le forfait le mieux noté par les membres</th>
<th>le forfait ayant le plus de réservations</th>
<th>le forfait le plus récent</th>
</tr>
<tr>
<td>@Model.CountForfaits</td>
<td>@Model.AverageCountForfaitPerAgence</td>
<td>@Html.ActionLink("Ici", "GetById", "Agence", new { numSiretAgence = Model.IdHighestPriceForfait }, null)</td>
<td>@Html.ActionLink("Ici", "GetById", "Agence", new { numSiretAgence = Model.IdLowestPriceForfait }, null)</td>
<td>@Html.ActionLink("Ici", "GetById", "Agence", new { numSiretAgence = Model.IdMostVisitedForfait }, null)</td>
<td>@Html.ActionLink("Ici", "GetById", "Agence", new { numSiretAgence = Model.IdBestRatedForfait }, null)</td>
<td>@Html.ActionLink("Ici", "GetById", "Agence", new { numSiretAgence = Model.IdMostBookedForfait}, null)</td>
<td>@Html.ActionLink("Ici", "GetById", "Agence", new { numSiretAgence = Model.IdMostRecentForfait}, null)</td>
</tr>

</table>

It's a table in which I put statistics. What I want to do is to have these statistics loaded in a web page based on a Form , the form will contain a simple dropdownlist in which the user choses the type of statistics that he wants. Here is the code of the partial View Action :

[HttpPost]
        public PartialViewResult _GetForfaitDashBoard (TypeForfait typeForfait)
        {
            .....
        }

Now I don't know how to do to put all I said into action. Is putting a form in the base view that posts to the action of the Partial View will be a good approach? Or is there any other solutions to call a partial view based on a form?

1 Answer 1

1

Since you're returning a PartialView my suggestion would be:

Use the jQuery Change event to get the selected value from your dropdownlist and post it to the server.

$("#yourdropdownid").change(function(e){}

Then post your data (my personal preference is via a Ajax request but you can also use the Post function) to your _GetForfaitDashBoard action and handle the response in your javascript:

$("#yourdropdownid").change(function(e){
    var selectedValue = this.val();

    $.ajax({
       url: $("#yourFormId").attr("action"),
       type: "POST",
       data: { typeForfait: selectedValue }, 
       success: function(response){ $('#IdOfTheElementWhereYouWantToInsert').html(response) },
       error: function(){ // handle your error }
    });
}

The response parameter in the success function is the rendered html of the partialView.

Assuming that you're basing the type of statistics only on the selected value I would suggest to change your parameter type from TypeForfait to string (or an int) in your action.

    [HttpPost]
    public PartialViewResult _GetForfaitDashBoard (string typeForfait)
    {

    }
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the reponse, this seems the right approach, but one more question, how can I inject the partial view in the page, I mean what shall I put in // inject you partial view on the page ? Sorry I am new to jquery ..
No problem, will edit my answer. You can do it with the .html() function of jQuery: api.jquery.com/html
also there is a little problem url: $("#yourFormId").attr("href") should be url: $("#yourFormId").attr("action") you can edit the answer for someone who has the same question :)
You can also put in the url in your javascript and remove the form, then you don't need this statement at all. But that's a personal preference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.