asp.NET 5 MVC 中的局部视图控制器

Controller for partial view in asp.NET 5 MVC

我想在表单中设置部分视图。 问题是我希望这个部分视图由控制器控制。

下面是表格的代码:

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

控制器代码如下:

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

        return PartialView(bdd);
    }

这是部分视图的代码:

@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>
}

有人可以帮助我吗?

在这段代码中输入 Your controller Nameload('/ControllerName/BudgetSearch')

完整代码

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


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

和其他方式

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

您的观点

<select asp-for="Categorie" id="categorie">
    @Html.Partial("BudgetSearch")
</select>
/*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);
    }

使用@Html.Action("MyController","BudgetSearch"),这将调用Controller Action和return内联相关视图,有些人声称它不是纯MVC,但我以前成功使用过它。