簡體   English   中英

Controller 用於 asp.NET 5 MVC 中的部分視圖

[英]Controller for partial view in asp.NET 5 MVC

我想在表單中設置部分視圖。 問題是我希望這個部分視圖由 controller 控制。

這是表單的代碼:

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

這是 controller 的代碼:

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") ,這將調用 Controller 動作並內聯返回相關視圖,有人聲稱它不是純 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM