1

Iwould like to set a partial View in a form. The problem is that I would like this partial View to be controlled by a controller.

Here is the code of the form :

<select asp-for="Categorie" id="categorie">
    <partial name="BudgetSearch" />
</select>

Here is the code of the controller :

public IActionResult BudgetSearch()
    {
        var bdd = new ComptesBudgetViewModel();

        return PartialView(bdd);
    }

Here is the code of the partia View :

@model Comptes.core.data.DataLayer.HomeDataLayer.ComptesBudgetViewModel

<option value="" selected>Choisir</option>
@foreach (var budget in Model.Budget)
{
    <option value="@budget.Categorie">@budget.Categorie</option>
}

Can someone help me ?

  • you need to call action that renders your partial – demo Feb 19 '21 at 11:56
  • Does this answer your question? [ASP.NET MVC 5: How do you render a partial view in a div from an action link?](https://stackoverflow.com/questions/34233863/asp-net-mvc-5-how-do-you-render-a-partial-view-in-a-div-from-an-action-link) – demo Feb 19 '21 at 11:57

3 Answers3

0

enter Your controller Name into this code: load('/ControllerName/BudgetSearch')

full code

<select asp-for="Categorie" id="categorie">
     
</select>


@section scripts{
    <script>
       $('#categorie').load('/ControllerName/BudgetSearch');
    </script>
}

And Other Way

public IActionResult MYAction()
{
   var bdd = new ComptesBudgetViewModel();
   return View(bdd);
}

Your View

<select asp-for="Categorie" id="categorie">
    @Html.Partial("BudgetSearch")
</select>
Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17
0
/*to load the partial view in main view on click of button*/
$("#btnId").on("click", function () {                          //on button click function using its id
    var suppName = document.getElementById("searchTxt").value;     //to get value of textbox using its id

   /*.get is used to get the partial view having 3 parameter one is url to the partial view
    * 2nd is variable value which you want to pass to controller and same name should be used in controller here in { name: name} 1st name is name should include in controller and 2nd one is variable name
    * 3rd is function to get data in our view #divContaiPopup is the div or place where partial view will be loaded*/
    $.get('@Url.Action("methodNameinController")', { name: name}, function (data) {
        $("#divContaiPopup").html(data);
  });

});



public ActionResult methodNmae(string name)
    {
        var data= new modelClass()
        { 
            modelclass = db.modelname.Where(a=> a.dbTableName.fieldName.Contains(name)).ToList(),
            
        };
        return View("PartialView", data);
    }
firoz khan
  • 27
  • 7
0

Use @Html.Action("MyController","BudgetSearch"), this will call the Controller Action and return the related view inline, some claim its not pure MVC, but I have used it with success before.

mxmissile
  • 11,464
  • 3
  • 53
  • 79