سؤال

دعونا نقول لدي الدرجة

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

على الصفحة أن لا يقع في مجلد العناصر ، حيث ItemController يتواجد أريد إنشاء رابط Login الأسلوب.حتى التي Html.ActionLink الطريقة يجب أن تستخدم وما المعلمات يجب أن تمر ؟

على وجه التحديد, أنا أبحث عن استبدال طريقة

Html.ActionLink(article.Title,
    new { controller = "Articles", action = "Details",
          id = article.ArticleID })

هذا وقد تقاعد مؤخرا ASP.NET MVC التجسد.

هل كانت مفيدة؟

المحلول

أعتقد أن ما تريده هو هذا:

ASP.NET MVC1

Html.ActionLink(article.Title, 
                "Login",  // <-- Controller Name.
                "Item",   // <-- ActionMethod
                new { id = article.ArticleID }, // <-- Route arguments.
                null  // <-- htmlArguments .. which are none. You need this value
                      //     otherwise you call the WRONG method ...
                      //     (refer to comments, below).
                )

يستخدم هذا الأسلوب التالي ActionLink التوقيع:

public static string ActionLink(this HtmlHelper htmlHelper, 
                                string linkText,
                                string controllerName,
                                string actionName,
                                object values, 
                                object htmlAttributes)

ASP.NET MVC2

اثنين من الحجج وقد تم تبديل جميع أنحاء

Html.ActionLink(article.Title, 
                "Item",   // <-- ActionMethod
                "Login",  // <-- Controller Name.
                new { id = article.ArticleID }, // <-- Route arguments.
                null  // <-- htmlArguments .. which are none. You need this value
                      //     otherwise you call the WRONG method ...
                      //     (refer to comments, below).
                )

يستخدم هذا الأسلوب التالي ActionLink التوقيع:

public static string ActionLink(this HtmlHelper htmlHelper, 
                                string linkText,
                                string actionName,
                                string controllerName,
                                object values, 
                                object htmlAttributes)

ASP.NET MVC3+

الحجج في نفس الترتيب كما MVC2 ، ومع ذلك معرف قيمة لم يعد المطلوبة:

Html.ActionLink(article.Title, 
                "Item",   // <-- ActionMethod
                "Login",  // <-- Controller Name.
                new { article.ArticleID }, // <-- Route arguments.
                null  // <-- htmlArguments .. which are none. You need this value
                      //     otherwise you call the WRONG method ...
                      //     (refer to comments, below).
                )

هذا يتجنب الثابت-ترميز أي منطق التوجيه في الارتباط.

 <a href="/Item/Login/5">Title</a> 

هذا وسوف تعطيك التالية إخراج html, على افتراض:

  1. article.Title = "Title"
  2. article.ArticleID = 5
  3. لا يزال لديك التالية في المسار المحدد

. .

routes.MapRoute(
    "Default",     // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

نصائح أخرى

وكنت أريد أن أضيف إلى جوزيف Kingry في الإجابة . وقدم الحل ولكن في البداية لم أتمكن من الحصول على عمل سواء وحصلت نتيجة لذلك تماما مثل Adhip غوبتا. ثم أدركت أن الطريق له وجود في المقام الأول، والمعلمات بحاجة إلى تطابق الطريق بالضبط. لذلك كان لي معرف ومن ثم معلمة نص طريقي التي تحتاج أيضا إلى أن تدرج أيضا.

Html.ActionLink(article.Title, "Login", "Item", new { id = article.ArticleID, title = article.Title }, null)

وأنت قد ترغب في النظر في في RouteLink() method.That واحد يتيح لك تحديد كل شيء (باستثناء نص الارتباط واسم الطريق) عن طريق القاموس.

وأعتقد أن يوسف انقلبت تحكم والعمل. يأتي أولا العمل ثم وحدة تحكم. هذا أمر غريب بعض الشيء، ولكن الطريقة التي ينظر التوقيع.

وفقط لتوضيح الأمور، وهذا هو الإصدار الذي يعمل (التكيف المثال يوسف):

Html.ActionLink(article.Title, 
    "Login",  // <-- ActionMethod
    "Item",   // <-- Controller Name
    new { id = article.ArticleID }, // <-- Route arguments.
    null  // <-- htmlArguments .. which are none
    )

وماذا عن هذا

<%=Html.ActionLink("Get Involved", 
                   "Show", 
                   "Home", 
                   new 
                       { 
                           id = "GetInvolved" 
                       }, 
                   new { 
                           @class = "menuitem", 
                           id = "menu_getinvolved" 
                       }
                   )%>
Html.ActionLink(article.Title, "Login/" + article.ArticleID, 'Item") 

إذا كنت تريد أن تذهب كل السراويل يتوهم، وهنا كيف يمكن أن تمتد لتكون قادرة على القيام بذلك:

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

وسوف تحتاج لوضع هذا في مساحة الاسم System.Web.Mvc:

public static class MyProjectExtensions
{
    public static MvcHtmlString ActionLink<TController>(this HtmlHelper htmlHelper, Expression<Action<TController>> 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<TController, TAction>(this HtmlHelper htmlHelper, Expression<Action<TController, TAction>> 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<TController>(this HtmlHelper htmlHelper, Expression<Action<TController>> 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<string, object> AnonymousObjectToKeyValue(object anonymousObject)
    {
        var dictionary = new Dictionary<string, object>();

        if (anonymousObject == null) return dictionary;

        foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(anonymousObject))
        {
            dictionary.Add(propertyDescriptor.Name, propertyDescriptor.GetValue(anonymousObject));
        }

        return dictionary;
    }
}

وهذا يشمل اثنين من تجاوزات لRoute Values وHTML Attributes، أيضا، سوف تحتاج كل وجهات النظر لإضافة: @using YourProject.Controllers أو يمكنك إضافته إلى web.config <pages><namespaces> بك

استخدم اسمه المعلمات من أجل قراءة ولتجنب الالتباس.

@Html.ActionLink(
            linkText: "Click Here",
            actionName: "Action",
            controllerName: "Home",
            routeValues: new { Identity = 2577 },
            htmlAttributes: null)

ومع MVC5 لقد فعلت ذلك من هذا القبيل وهو 100٪ كود العمل ....

@Html.ActionLink(department.Name, "Index", "Employee", new { 
                            departmentId = department.DepartmentID }, null)

ويا رفاق يمكن الحصول على فكرة من هذا ...

وهذا نوع الاستخدام:

و @Html.ActionLink ( "مشارك تحميل الصفحة"، "مؤشر"، "الوطن")

ومشارك تحميل الصفحة: اسم النص الفهرس: العمل الشخصي الرئيسية: HomeController

واستخدام قاعدة ActionLink

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>_Layout</title>
    <link href="@Url.Content("~/Content/bootsrap.min.css")" rel="stylesheet" type="text/css" />
</head>
<body>
    <div class="container">
        <div class="col-md-12">
            <button class="btn btn-default" type="submit">@Html.ActionLink("AnaSayfa","Index","Home")</button>
            <button class="btn btn-default" type="submit">@Html.ActionLink("Hakkımızda", "Hakkimizda", "Home")</button>
            <button class="btn btn-default" type="submit">@Html.ActionLink("Iletişim", "Iletisim", "Home")</button>
        </div> 
        @RenderBody()
        <div class="col-md-12" style="height:200px;background-image:url(/img/footer.jpg)">

        </div>
    </div>
</body>
</html>

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top