开发者

using html.actionlink outside of a viewpage in MVC3

开发者 https://www.devze.com 2023-04-11 07:45 出处:网络
Struggling on in my first MVC project, I need to show a menu of steps for a wizard. The problem is that some of the steps depend on the data that the user provides.

Struggling on in my first MVC project, I need to show a menu of steps for a wizard. The problem is that some of the steps depend on the data that the user provides. So say for example, if a user pays cash, we skip the page that shows bank-account information (and thus remove the menu-item)..

I have a "page" class in C#, and a "menu" class that has a list of "pages". I'm going to write the logic that dictates whether or not to show a menu-item in the menu class.

The problem is this; The page class is a very simple class that looks like this ("active" means the menuitem is highlighted as the currently active item):

public class Page
{
    MvcHtmlString Link { get; set; }
    bool Active { get; set; }
    bool Visible { get; set; }

    public Page(MvcHtmlString link)
    {
        Link = link;
        Active = false;
        Visible = true;
    }

    public Page(MvcHtmlString link, bool active, bool visible)
    {
        Link = link;
        Active = active;
        Visible = visible;
    }
}

In my logic I want to add pages to the list, and I'd like to use the "Link" property of a page to store the target URL for each menu-item. This way I can fill that property using Html.Actionlink() and all its available parameters to generate the HTML for the link. In the eventual menu I can just iterate and show the items. It also has the advantage that I could do an AJAX call to this function and receive HTML back I can use in my page to update the menu dynamically.

However, Html.ActionLink is not available, and in other posts on Stackoverflow I found that people also dislike using a html helper outside of an MVC view, for understandable reasons.

So my qu开发者_StackOverflow中文版estion is; how would I go about this in a clean solution? I considered giving the page class not a Link property, but "controller" and "action" properties, but I would be missing out on the flexibility of adding a class or other HTML attributes easily, which the actionlink DOES provide.

Thanks,

Erik

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号