簡體   English   中英

如何在ASP.NET MVC中的View中調用多個動作?

[英]How to call multiple actions in View in ASP.NET MVC?

問題是:

我正在使用文本框來獲取字符串q,並希望將其傳遞給search控制器中的3個不同的動作。 action1(string q), action2(string q)等等

現在我的動作語法:

 public ActionResult action1(string q)
  {
   var mydata = from p in fab //LINQ logic
                select new action1class
                { data1=p //assignment };
   return View("_partialAction1", mydata);
  }

同樣,還有兩個其他動作。

我使用3種不同的動作,因為我的LINQ邏輯從3種不同的來源獲取數據,因此需要創建不同的mydata

我的問題是:我試圖當我單擊文本框的“搜索”按鈕時 ,所有這3個動作都應運行並在某些<div id="action1">標記中生成一個在另一個下方的局部視圖。

我嘗試使用ajax.BeginForm但一次只能調用一個動作

@using (Ajax.BeginForm("action1", "Search", new AjaxOptions
{
    HttpMethod = "GET",
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "action1",
    LoadingElementId="progress"
}))

我也嘗試使用ViewModel但是問題是我無法將更大的模型以及在LINQ中獲得的mydata類數據一起傳遞給視圖。 我不知道在這種情況下如何使用viewmodel。

我使用的方法正確嗎? 還是可以有其他方法嗎? 我想通過單擊按鈕顯示所有操作的結果。

MVC框架中有兩種類型的動作。 第一個是主要動作 ,它們一次從瀏覽器中調用。 第二種類型稱為“ 子動作” ,這些動作不能從瀏覽器調用,而只能從主要動作返回的視圖中調用。 可以在一個主要動作下調用多個 動作 因此,無論是否有幫助,您都必須研究兒童行為。

例如

// main action that returns a view
public ViewResult Index()
{
   var model = ...
   return View(model);
}

// couple of child actions each returns a partial view
// which will be called from the index view
[ChildActionOnly]
public PartialViewResult ChildAction1()
{
  var model = ...
  return PartialView(model);
}

[ChildActionOnly]
public PartialViewResult ChildAction2()
{
  var model = ...
  return PartialView(model);
}

// index view
Index.cshtml
@model ...

@Html.Action("ChildAction1");
@Html.Action("ChildAction2");

...

http://msdn.microsoft.com/en-us/library/ee839451.aspx

每個請求只能執行一項操作。 如果您希望一次單擊具有3個不同的局部視圖,則需要構造一個布局頁面,其中包含3個局部視圖的顯示方式,並確保您的操作收到正確的參數以執行所有局部視圖渲染。

為什么不將ViewModel傳遞給partialViews。 確保您在ViewModel中具有不同的屬性,以保存PartialView Specific數據以及搜索文本。 這是一個例子:

模型

public class Product
{
    public string Name { get; set; }
    public string Type { get; set; }
    public string Class { get; set; }
}

視圖模型

public class ProductSearch
{
    public ProductSearch()
    {
        q = string.Empty;
        Product1 = new Product();
        Product2 = new Product();
    }
    public string q { get; set; }
    public Product Product1 { get; set; }
    public Product Product2 { get; set; }
}

_Partial1.cshtml

@model Test1.Models.ProductSearch

<div>Product1</div>  

@Html.TextBoxFor(a => a.Product1.Name)

_Partial2.cshtml

@model Test1.Models.ProductSearch

<div>Product2</div>  

@Html.TextBoxFor(a => a.Product2.Name)

ActualView.cshtml

@model Test1.Models.ProductSearch

@{
    ViewBag.Title = "ActualView";
}

<h2>ActualView</h2>

@using (Html.BeginForm())
{
    @:SearchText
    @Html.TextBoxFor(m => m.q)
    Html.RenderAction("_Partial1", Model);
    Html.RenderAction("_Partial2", Model);
    <input type="submit" runat="server" id="btnSubmit" />
}

臨時數據 (您將從數據庫/任何其他來源獲取它)

private List<Product> ProductsToSearch()
{
     return new List<Product>() { new Product() { Name = "Product One", Class = "A", Type = "High" }, new Product() { Name = "Product Two", Class = "A", Type = "Low" }, new Product() { Name = "Product Three", Class = "B", Type = "High" } };
}

控制器動作

    public ActionResult _Partial1(ProductSearch search)
    {
        Product Product1 = ProductsToSearch().Where(a => a.Class.Equals(search.q) && a.Type.Equals("High")).SingleOrDefault();
        search.Product1 = Product1;
        return PartialView(search);
    }

    public ActionResult _Partial2(ProductSearch search)
    {
        Product Product2 = ProductsToSearch().Where(a => a.Class.Equals(search.q) && a.Type.Equals("Low")).SingleOrDefault();
        search.Product2 = Product2;
        return PartialView(search);
    }

    [HttpPost]
    public ActionResult ActualView(ProductSearch search)
    {
        return View(search);
    }

    public ActionResult ActualView()
    {
        ProductSearch search = new ProductSearch();           
        return View(search);
    }

現在,如果您為SearchText輸入'A'並點擊Submit Query您將得到兩個不同的結果(基本上使用通用的搜索文本,並且基於每個局部視圖中的搜索查詢,它會生成不同的結果)

在此處輸入圖片說明

暫無
暫無

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

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