HTML.ActionLink method

前端 未结 10 1605
刺人心
刺人心 2020-11-22 11:07

Let\'s say I have a class

public class ItemController:Controller
{
    public ActionResult Login(int id)
    {
        return View(\"Hi\", id);
    }
}
         


        
10条回答
  •  余生分开走
    2020-11-22 11:26

    If you want to go all fancy-pants, here's how you can extend it to be able to do this:

    @(Html.ActionLink(x => x.Details(), article.Title, new { id = article.ArticleID }))
    

    You will need to put this in the System.Web.Mvc namespace:

    public static class MyProjectExtensions
    {
        public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, Expression> expression, string linkText)
        {
            var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection);
    
            var link = new TagBuilder("a");
    
            string actionName = ExpressionHelper.GetExpressionText(expression);
            string controllerName = typeof(TController).Name.Replace("Controller", "");
    
            link.MergeAttribute("href", urlHelper.Action(actionName, controllerName));
            link.SetInnerText(linkText);
    
            return new MvcHtmlString(link.ToString());
        }
    
        public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, Expression> expression, string linkText, object routeValues)
        {
            var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection);
    
            var link = new TagBuilder("a");
    
            string actionName = ExpressionHelper.GetExpressionText(expression);
            string controllerName = typeof(TController).Name.Replace("Controller", "");
    
            link.MergeAttribute("href", urlHelper.Action(actionName, controllerName, routeValues));
            link.SetInnerText(linkText);
    
            return new MvcHtmlString(link.ToString());
        }
    
        public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, Expression> expression, string linkText, object routeValues, object htmlAttributes) where TController : Controller
        {
            var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection);
    
            var attributes = AnonymousObjectToKeyValue(htmlAttributes);
    
            var link = new TagBuilder("a");
    
            string actionName = ExpressionHelper.GetExpressionText(expression);
            string controllerName = typeof(TController).Name.Replace("Controller", "");
    
            link.MergeAttribute("href", urlHelper.Action(actionName, controllerName, routeValues));
            link.MergeAttributes(attributes, true);
            link.SetInnerText(linkText);
    
            return new MvcHtmlString(link.ToString());
        }
    
        private static Dictionary AnonymousObjectToKeyValue(object anonymousObject)
        {
            var dictionary = new Dictionary();
    
            if (anonymousObject == null) return dictionary;
    
            foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(anonymousObject))
            {
                dictionary.Add(propertyDescriptor.Name, propertyDescriptor.GetValue(anonymousObject));
            }
    
            return dictionary;
        }
    }
    

    This includes two overrides for Route Values and HTML Attributes, also, all of your views would need to add: @using YourProject.Controllers or you can add it to your web.config

提交回复
热议问题