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

有人可以帮我吗?

解决方法

/*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"),这将调用控制器操作并内联返回相关的视图,有人声称它不是纯MVC,但我之前成功使用过它。

,

在此代码中输入 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>