[英]How do I create an action link for each action in my controller within a view using asp.net mvc?
SO, I have 20+ Action
s in a controller as here below: 所以,我在控制器中有20多个
Action
,如下所示:
public ActionResult DoThis()
{
Image img = Image.FromFile("DoThis.png");
//some other stuff to be done.
return View();
}
public ActionResult DoThat()
{
Image img = Image.FromFile("DoThat.png");
//some other stuff to be done.
return View();
}
I have a view which I want to show ActionLink
for each action that is placed in my controller with assigned picture to the action, let say this: 我有一个视图,我想为放置在控制器中的每个操作显示
ActionLink
,并为该操作分配图片,可以这样说:
@foreach
{
<div class="col-sm-4">
<div class="product-image-wrapper">
<div class="single-products">
<div class="productinfo text-center">
<img src="the image assigned to the controller actions" alt="" />
<h3 style="font-family:'Myriad عربي'">Action Name ؟</h3>
<a href="@URL.Action("The acion link from controller")" class="btn btn-default add-to-cart">Click Here</a>
</div>
</div>
</div>
</div>
}
Is that possible or I am asking too much? 那可能吗,或者我要求太多?
Here's one way to get a list of ActionDescriptor[]
for all actions on your controller: 这是获取控制器上所有动作的
ActionDescriptor[]
列表的一种方法:
@{
var descriptor = new ReflectedControllerDescriptor(this.ViewContext.Controller.GetType());
var actions = descriptor.GetCanonicalActions();
}
@foreach (var action in actions)
{
<div>
@Html.ActionLink("click me", action.ActionName)
</div>
}
Now of course this will get a list of all actions on the current controller. 当然,现在,这将获得当前控制器上所有动作的列表。 If on the other hand you want to get only some specific actions, you could decorate them with some marker attribute and then filter the retrieved actions to only those having the attribute:
另一方面,如果您只想获取某些特定的动作,则可以用一些marker属性装饰它们,然后将检索到的动作过滤为仅具有该属性的动作:
public class MyActionAttribute: Attribute
{
}
...
public class HomeController: Controller
{
public ActionResult Index()
{
return View();
}
[MyAction]
public ActionResult DoThis()
{
Image img = Image.FromFile("DoThis.png");
//some other stuff to be done.
return View();
}
[MyAction]
public ActionResult DoThat()
{
Image img = Image.FromFile("DoThat.png");
//some other stuff to be done.
return View();
}
}
and then: 接着:
@{
var descriptor = new ReflectedControllerDescriptor(this.ViewContext.Controller.GetType());
var actions = descriptor.GetCanonicalActions().Where(desc => desc.GetCustomAttributes(typeof(MyActionAttribute), true).Any());
}
Well, you can certainly get the methods of a certain class via reflection: 好吧,您当然可以通过反射来获取特定类的方法:
public List<string> GetActionNames()
{
return typeof(HomeController)
.GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
.Where(method => method.ReturnType.IsSubclassOf(typeof(ActionResult)) || method.ReturnType == typeof(ActionResult))
.Select(method => method.Name)
.ToList();
}
This will return the names of all actions defined on your controller, where anything returning an ActionResult
-compatible type is considered an action method and nothing else is. 这将返回控制器上定义的所有动作的名称,其中任何返回与
ActionResult
兼容的类型的东西都被视为动作方法,而没有别的东西。 However, you should consider the following: 但是,您应该考虑以下几点:
HttpPost
attribute on them (this gets even more complicated if you are dealing with an ApiController
, where you should also consider the name prefix based method convention). HttpPost
属性的方法来过滤方法(如果要处理ApiController
,这会变得更加复杂)考虑基于名称前缀的方法约定)。 Generally speaking, the first bulletpoint is of most concern, you will be better off by just typing the links by hand rather than trying to figure out a reliable and general way of solving it. 一般而言,第一个要点是最令人关注的问题,您只需手动输入链接,而不是尝试找出一种可靠且通用的解决方法,就会更好。 So I would only advise you to use such a logic if you can be sure that the aforementioned things are not something that you have to care about.
因此,我只建议您在可以确定不必担心上述问题的情况下使用这种逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.