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

时光毁灭记忆、已成空白 提交于 2021-02-18 19:10:04

问题


Problem is:

I am using a textbox to get a string q and want to pass it to 3 different actions in search controller. i.e. action1(string q), action2(string q) and so on

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.

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.

I tried to use ajax.BeginForm but it can only call one action at a time

@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. I have no clear idea of how to use viewmodel in this case.

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.


回答1:


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




回答2:


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:


Why not pass the ViewModel to the partialViews. Make sure you have different properties in the ViewModel to hold the PartialView Specific data plus the search text. 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

@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" />
}

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)

enter image description here



来源:https://stackoverflow.com/questions/10915485/how-to-call-multiple-actions-in-view-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!