1

问题是:

我正在使用文本框获取字符串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需要创建不同的需求。

我的问题是:我正在尝试当我单击文本框的“搜索”按钮<div id="action1">时,所有 3 个操作都应该运行并在某些标签中生成位于其他下方的部分视图。

我尝试使用ajax.BeginForm,但一次只能调用一个动作

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

我也尝试使用ViewModel,但问题是我无法将更大的模型与mydata在 LINQ 中获得的这些数据一起传递给视图。在这种情况下,我不清楚如何使用视图模型。

我使用的方法是否正确?或者还有其他方法吗?我想通过单击按钮显示所有操作的结果。

4

3 回答 3

6

There are two types of actions are in MVC framework. The first ones are the main actions and they are invoked from the browser one at a time. The second type are called as Child Actions and these actions can't be invoked from the browser but from the views returned by the main actions. Multiple child actions can be called under a main action. So you have to look into child actions whether they help or not.

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

于 2012-06-06T14:15:13.547 回答
0

为什么不将 ViewModel 传递给 partialViews。确保 ViewModel 中有不同的属性来保存 PartialView 特定数据和搜索文本。这是一个例子:

模型

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)

实际视图.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,您将得到两个不同的结果(基本上使用常见的搜索文本,并且基于每个局部视图中的搜索查询,它会生成不同的结果)

在此处输入图像描述

于 2012-06-06T16:07:30.570 回答
0

每个请求只能执行一项操作。如果您希望一次单击获得 3 个不同的局部视图,您将需要构建一个布局页面,其中包含您想要的 3 个局部视图,并确保您的操作接收正确的参数以执行所有局部视图渲染.

于 2012-06-06T13:49:19.853 回答