2010-05-24 15 views
10

क्या कोई समझा सकता है कि निम्नलिखित क्यों होता है? और कैसे हल करने के लिए,Asp.Net एमवीसी एक्शनलिंक

में

/उत्पाद/AddOption? वर्ग = लाइटबॉक्स

<%= Html.ActionLink("Add New Option", "AddOption", "Product", new { @class = "lighbox" })%> 

परिणाम में दृश्य स्टूडियो 2010 और MVC2

<%= Html.ActionLink("Add New Option", "AddOption", "Product", new { @class = "lighbox" }, null)%> 

परिणाम/उत्पाद/AddOption? लंबाई = 7

धन्यवाद

उत्तर

20

आप इन संबंधित भार के प्रयोग कर रहे हैं:

public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper, 
string linkText, 
string actionName, 
string controllerName, 
Object routeValues, 
Object htmlAttributes 
) 
से

: http://msdn.microsoft.com/en-us/library/dd504972.aspx

public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper, 
string linkText, 
string actionName, 
Object routeValues, 
Object htmlAttributes 
) 

से: http://msdn.microsoft.com/en-us/library/dd492124.aspx

पहले new { @class = "lighbox" } पारित हो जाता है routeValues तर्क के रूप में जब यह htmlAttributes तर्क होना चाहिए।

इस प्रकार की समस्या एमवीसी में उपयोग की जाने वाली विस्तार विधियों के साथ आम है।

<%= Html.ActionLink(linkText: "Add New Option", 
    actionName: "AddOption", 
    controllerName: "Product", 
    htmlAttributes: new { @class = "lighbox" }, 
    routeValues: null)%> 
9

यह "अधिभार नरक" ASP.NET MVC में का एक उदाहरण है: सकते हैं कभी कभी चीजें अधिक पठनीय बनाने के लिए नामित तर्क (सी # 4.0) का उपयोग करने के लिए मदद। जबकि दूसरा कोड कहता है इस एक

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper, 
    string linkText, 
    string actionName, 
    string controllerName, 
    Object routeValues, 
    Object htmlAttributes 
) 

:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper, 
    string linkText, 
    string actionName, 
    Object routeValues, 
    Object htmlAttributes 
) 

सूचना है कि पहली कॉल में स्ट्रिंग पैरामीटर controllerName दूसरे में routeValues होता जा रहा है

पहले कोड निम्न विधि कॉल एक। स्ट्रिंग मान "उत्पाद" मार्गित मानों को पारित किया जा रहा है: स्ट्रिंग प्रॉपर्टी Length का उपयोग किया जाता है, जिसमें इसकी लंबाई 7 है, इसलिए "लंबाई = 7" आप मार्ग में जा रहे हैं।

पहली विधि को ध्यान में रखते हुए, ऐसा लगता है कि आपने routeValues और htmlAttributes पैरामीटर को बदल दिया है।

संबंधित मुद्दे