简体   繁体   English

如何在ASP.NET MVC中的View中调用多个动作?

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

Problem is: 问题是:

I am using a textbox to get a string q and want to pass it to 3 different actions in search controller. 我正在使用文本框来获取字符串q,并希望将其传递给search控制器中的3个不同的动作。 ie action1(string q), action2(string q) and so on action1(string q), action2(string q)等等

Now syntax of my action: 现在我的动作语法:

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

Similarly there are two other actions. 同样,还有两个其他动作。

I am using 3 different actions because my LINQ logic gets data from 3 different sources so there different mydata needs to be created. 我使用3种不同的动作,因为我的LINQ逻辑从3种不同的来源获取数据,因此需要创建不同的mydata

My problem is: I am trying that when I click on 'search' Button of textbox then all the 3 actions should run and generate partial view one below other in some <div id="action1"> tags. 我的问题是:我试图当我单击文本框的“搜索”按钮时 ,所有这3个动作都应运行并在某些<div id="action1">标记中生成一个在另一个下方的局部视图。

I tried to use ajax.BeginForm but it can only call one action at a time 我尝试使用ajax.BeginForm但一次只能调用一个动作

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

Also I tried to use ViewModel but the problem is that I was unable to pass a bigger model to the view along with these mydata kind of data obtained in LINQ's in the action. 我也尝试使用ViewModel但是问题是我无法将更大的模型以及在LINQ中获得的mydata类数据一起传递给视图。 I have no clear idea of how to use viewmodel in this case. 我不知道在这种情况下如何使用viewmodel。

Is the approach that I am using correct? 我使用的方法正确吗? Or can there be any other way? 还是可以有其他方法吗? I want to show result of all actions with button click. 我想通过单击按钮显示所有操作的结果。

There are two types of actions are in MVC framework. MVC框架中有两种类型的动作。 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 http://msdn.microsoft.com/en-us/library/ee839451.aspx

You can only have one action per request. 每个请求只能执行一项操作。 If you want to have 3 different partial views for a singular click, you will need to construct a layout page that includes the 3 partial views how you want them and make sure that your action receives the proper parameters to perform all of the partial view rendering. 如果您希望一次单击具有3个不同的局部视图,则需要构造一个布局页面,其中包含3个局部视图的显示方式,并确保您的操作收到正确的参数以执行所有局部视图渲染。

Why not pass the ViewModel to the partialViews. 为什么不将ViewModel传递给partialViews。 Make sure you have different properties in the ViewModel to hold the PartialView Specific data plus the search text. 确保您在ViewModel中具有不同的属性,以保存PartialView Specific数据以及搜索文本。 Here is an example: 这是一个例子:

Model 模型

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

ViewModel 视图模型

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 _Partial1.cshtml

@model Test1.Models.ProductSearch

<div>Product1</div>  

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

_Partial2.cshtml _Partial2.cshtml

@model Test1.Models.ProductSearch

<div>Product2</div>  

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

ActualView.cshtml 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" />
}

Temp Data (you will be getting it from DB/ any other source) 临时数据 (您将从数据库/任何其他来源获取它)

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

Controller Actions 控制器动作

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

Now if you enter 'A' for SearchText and hit Submit Query you will get two different results (basically common search text is used and based on the search query in each partial view it has generated different results) 现在,如果您为SearchText输入'A'并点击Submit Query您将得到两个不同的结果(基本上使用通用的搜索文本,并且基于每个局部视图中的搜索查询,它会生成不同的结果)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM