简体   繁体   English

如何使用asp.net mvc在视图中为控制器中的每个动作创建动作链接?

[英]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: 但是,您应该考虑以下几点:

  • How will you decide what to bind to which parameter, 您将如何决定要绑定到哪个参数的内容,
  • How will you handle overloaded methods, 您将如何处理重载的方法,
  • You should refine this logic based on any security needs and other restrictions, 您应该根据任何安全需求和其他限制来完善此逻辑,
  • You should filter them further if any of the methods is a non-HttpGet, you could do this by filtering the methods by not having an 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). 如果任何方法都是非HttpGet方法,则应该进一步过滤它们,可以通过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.

相关问题 ASP.NET MVC-如何创建动作链接以适合控制器/ {id} /动作 - ASP.NET MVC - how to create action link to fit controller/{id}/action ASP.NET MVC如何将控制器操作指向其他视图? - ASP.NET MVC How do I point a controller action to a different view? ASP.NET MVC - 从视图中创建 Url 到 controller 动作的最佳方法是什么? - ASP.NET MVC - What's the best way to create a Url to controller action from within view? asp.net mvc如何将视图与控制器动作联系起来? - how does asp.net mvc relate a view to a controller action? 在asp.net mvc 4中,有没有一种方法可以在基本控制器中创建索引操作,并将该操作的视图共享给多个控制器? - is there a way in asp.net mvc 4 to create an Index Action inside Base Controller and Share the view of this Action to multiple Controllers? 如何在 Asp.net MVC 中使用 Ajax 命中控制器动作? - How To Hit Controller Action Using Ajax In Asp.net MVC? 如何基于ASP.NET MVC中的控制器操作在页面javascript中设置变量? - How can I set a variable in my pages javascript based on my controller action in ASP.NET MVC? 当我在asp.net mvc控制器操作中验证失败时,如何保留我的URL - how can i keep my url when my validation fail in asp.net mvc controller action ASP.NET MVC:是否真的必须为每个动作创建一个视图? - ASP.NET MVC: Do I really have to create one view per action? 如何接受数组作为 ASP.NET MVC 控制器操作参数? - How do I accept an array as an ASP.NET MVC controller action parameter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM