2012-05-14 82 views
4

在ASP.MVC 3或4(使用Razor)中,如何將一個CSS類應用於Url.Action()輔助方法?可能嗎?如何將CSS類名稱添加到ASP.NET MVC 3 Url.Action鏈接?

期望的結果:

<a href="home\index?page=2" class="FOO">BAR</a> 

我有這個迄今爲止得到:

@Url.Action("Index", "Home", new { page }) 

更新:感謝所有。我犯了幾個錯誤,我應該爲未來的讀者澄清(很可能是我自己)。我希望我可以給你一個以上的信用。

a)new {page} - 不要這樣做。它會產生不希望的輸出。我的意思是:ViewBag.page或ViewBag.pageNum

b)正如你們中的一些人所指出的那樣,我需要Html.ActionLink()而不是Url.Action(),我一直在使用它,直到我切換到生成完整標記,而不僅僅是網址。

我確認以下作品期望:

@Html.ActionLink("BAR", "Index", "Home", new { ViewBag.PageNum }, new { @class = "FOO" }) 
+0

類似於'new'中的內容:@ Url.Action(「Index」,「Home」,new {page,className =「foo」}) – MilkyWayJoe

+2

@MilkyWayJoe我認爲它的'新{@ class ='...'}'不是? – asawyer

+0

@MilkyWayJoe:我認爲這將追加&class = foo到url。下面的答案似乎是我需要使用ActionLink。 –

回答

11

我相信你想使用ActionLink代替。這將允許你添加你想要的任何屬性。

@Html.ActionLink("BAR", "Index", "Home", new { page }, new { @class = "FOO" }) 

輸出:

<a href="home/index?page=2" class="FOO">BAR</a> 

,或者你能做到 「手動」

<a href="@Url.Action("Index", "Home", new { page })" class="FOO">BAR</a> 
0

這是我做的:

我有這個類:

public class HtmlValues : Dictionary<string,object> 
{ 
    public HtmlValues Class(string ClassName) 
    { 
     if (this.ContainsKey("class")) 
     { 
      ClassName = String.Format("{0} {1}", this["class"], ClassName); 
      this.Remove("class"); 
     } 
     return this.WithValue("class", ClassName); 
    } 
    public HtmlValues Name(string Name) 
    { 
     return this.WithValue("name", Name); 
    } 
    public HtmlValues Style(string Style) 
    { 
     return this.WithValue("style", Style); 
    } 
    public HtmlValues MaxLength(int Length) 
    { 
     return this.WithValue("maxlength", Length.ToString()); 
    } 
    public HtmlValues RTL() 
    { 
     return this.WithValue("dir", "rtl"); 
    } 
    public HtmlValues With(string Attribute, string Value) 
    { 
     return this.WithValue(Attribute, Value); 
    } 

    public static HtmlValues WithClass(string CssClass) 
    { 
     return new HtmlValues().Class(CssClass); 
    } 

    public HtmlValues Data(string key, string value) 
    { 
     return this.WithValue("data-" + key, value); 
    } 
} 

有了這個擴展方法:

public static class Extensions 
{ 
    public static T WithValue<T>(this T dict, string key, object value) where T : IDictionary<string, object> 
    { 
     dict.Add(key, value); 
     return dict; 
    } 
} 

然後我的剃鬚刀是這樣的:

@Html.TextBoxFor(model => model.SomeProperty, HtmlValues.WithClass("SomeClass")) 

這似乎有點小題大做,但在實踐中相當不錯的,因爲它可以讓你連鎖添加屬性/值的元素在一個很好的流利,對讀者時尚有意義。

@Html.TextBoxFor(model => model.SomeProperty, 
    HtmlValues.WithClass("SomeClass") 
       .Class("someotherClass") 
       .Data("Some-Jquery-Data-Thing") 
       .With("Nonstandard-Attribute","Its-Value")) 
4

Url.Action不會產生任何HTML元素,只創建一個URL,這就是爲什麼它被稱爲Url.Action,而不是Html.Action ...(Html.Action是完全不同的東西)。

如果你想有一個鏈接的CSS您使用Url.Action上,只是這樣做:

<a href="@Url.Action("Action", "Controller")" class="myclass">My Link<a> 

或者,你可以使用Html.ActionLink

@Html.ActionLink("My Link", "Action", "Controller", null, new { @class="myclass" }) 
+0

關於Url.Action vs Html.ActionLink的好處。 –

1

的Url.Action幫手只會打印url而不是完整的錨點。

你有兩個選項:

@Html.ActionLink("BAR", "index","home", new { @class = "FOO" }) 
  • 注意使用的類爲類的@符號是保留關鍵字

和(使用Url.Action助手)

<a href="@Url.Action("Index", "Home", new { page })" class="FOO">BAR</a> 
+0

或什麼Mystere人說.... –

相關問題