2012-06-06 37 views
1

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

我使用一個文本框得到一個字符串q,並希望將它傳遞給3個不同的行動search控制器。即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調用一個動作,但問題是,我無法一個更大的模型傳遞到一起的觀點這些mydata種類的數據在LINQ的行動中獲得。在這種情況下,我不清楚如何使用viewmodel。

我正在使用的方法是否正確?或者還有其他方法嗎?我想用按鈕單擊來顯示所有操作的結果。

+1

你爲什麼想把它分解成3個動作? –

+0

我建議你再看看使用AJAX。 AJAX將允許IIS處理異步操作(請求),從而縮短響應時間。你能發佈你的AJAX代碼嗎? – Joshua

+0

@ DanielA.White使用3種不同的動作,因爲我的LINQ邏輯從3個不同的源獲取數據,因此需要創建3個不同的「mydata」。 – Man8Blue

回答

6

MVC框架中有兩種類型的動作。第一個是的主要操作,它們一次從瀏覽器中調用。第二種類型稱爲Child Actions,這些操作不能從瀏覽器調用,而是從主操作返回的視圖中調用。多個子動作可以通過主動作調用。所以你必須看看孩子的行爲是否有幫助。

Ex。

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

+0

好的例子Mark。鏈接被打破了。 – radbyx

0

每個請求只能有一個動作。如果您希望有三個不同的局部視圖用於單擊,則需要構建一個佈局頁面,其中包含3個部分視圖,並確保您的操作接收適當的參數以執行所有局部視圖呈現。

+0

我準備好了渲染事情......但如何通過一個按鈕單擊或一個Ajax請求來調用3個動作? – Man8Blue

+0

@ Man8Blue:你沒有。你打電話給1個動作。該行爲必須依次對應於包含您的部分視圖的單個視圖。請求 - >操作是1:1關係。 –

+0

@ Man8Blue:如果你想進入AJAX來完成這個,那肯定會有用,但它仍然會歸結爲1:1的概念。每個局部視圖需要1次AJAX調用。 –

0

爲什麼不通過視圖模型到partialViews。確保您在ViewModel中具有不同的屬性來保存部分視圖特定數據和搜索文本。下面是一個例子:

模型

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

現在,如果你輸入「A」的SearchText和打Submit Query你會得到兩種不同的結果(基本上是用來和基於每個局部視圖搜索查詢常用搜索文本已產生不同的結果)

enter image description here

相關問題