SORU
14 EKİM 2008, Salı


HTML.ActionLink metodu

Hadi bir sınıf var diyelim

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

ItemController bulunduğu Madde klasörde bulunan bir sayfa üzerinde,, Login yöntemi için bir bağlantı oluşturmak istiyorum. Ve parametreleri geçiyorum ne yapmalıyım? kullanmalıyım yani

Özellikle, bu yöntem yerine arıyorum

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

bu son ASP.NET MVC yaşamında da emekli olmuştur.

CEVAP
14 EKİM 2008, Salı


İstediğin şey sanırım bu

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).
                )

Bu aşağıdaki yöntemi ActionLink imza: kullanır

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

ASP.NET MVC2

iki argüman etrafında yer değiştirdi

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).
                )

Bu aşağıdaki yöntemi ActionLink imza: kullanır

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

ASP.NET MVC3

bağımsız MVC2, ıd değerini ancak artık gerekli olduğu gibi, aynı sırayla

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).
                )

Bu sabit kodlama bağlantıya herhangi bir yönlendirme mantığı engeller.

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

Bu aşağıdaki html çıktısını varsayarak verecektir:

  1. article.Title = "Title"
  2. article.ArticleID = 5
  3. yine de aşağıdaki yol tanımlamıştır

. .

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

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Chanre Joubert

    Chanre Joube

    27 Temmuz 2012
  • filmurfreakur

    filmurfreaku

    29 Mart 2007
  • Krumme1996

    Krumme1996

    21 EYLÜL 2009