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

编程入门 行业动态 更新时间:2024-10-20 00:27:51
本文介绍了如何在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"); ...

msdn.microsoft/en-us/library/ee839451.aspx

更多推荐

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

本文发布于:2023-11-24 21:04:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626819.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   动作   如何在   ASP   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!