本文介绍了如何在ASP.NET MVC中的View中调用多个动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是:

我正在使用文本框来获取字符串 q ,并希望将其传递给 search 控制器中的3个不同的动作.即 action1(string q),action2(string q)

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

我现在的动作语法:

 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.

我正在使用3种不同的动作,因为我的LINQ逻辑从3种不同的来源获取数据,因此需要创建不同的 mydata .

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个动作都应运行并在某些< div中生成下面一个的局部视图.id ="action1"> 标签.

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.

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

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

我也尝试使用 ViewModel ,但是问题是我无法将更大的模型以及在LINQ中获得的这些 mydata 数据一起传递给视图.那个行动.我不知道在这种情况下如何使用viewmodel.

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.

推荐答案

MVC框架中有两种类型的操作.第一个是主要动作,它们一次从浏览器中调用一次.第二种类型称为子操作和这些操作不能从浏览器中调用,而只能从主要操作返回的视图中调用.可以在一个主动作下调用多个子动作.因此,您必须研究儿童行为是否有帮助.

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.

例如.

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

这篇关于如何在ASP.NET MVC中的View中调用多个动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 01:24