2013-10-02 48 views
1

我已經localizaed ASP.NET MVC應用程序與觀點:ASP.NET MVC提交按鈕定位

<button type="submit" value="@Resources.Yes"> 
<button type="submit" value="@Resources.No"> 

和cotroller:

public ActionResult Index(..., string submit) 
{   
    switch (submit) 
    { 
     case "Yes":  
     default: 
      ..... 
      break; 
     case "No": 
      .... 
      break; 
    } 
} 

但它的工作原理很明顯只爲英語。你如何解決多種語言的提交按鈕檢測?

我發現文章http://blog.maartenballiauw.be/post/2009/11/26/Supporting-multiple-submit-buttons-on-an-ASPNET-MVC-view.aspx,他們使用attributs,但我不能使用它,因爲本地化不是恆定的。

回答

1

在您的視圖模型,包括每個按鈕串屬性:

public class MyViewModel 
{ 
    public string YesButton { get; set; } 
    public string NoButton { get; set; } 
} 

在視圖:

<input type="submit" name="@Html.NameFor(m => m.YesButton)" value="@Resources.Yes" /> 
<input type="submit" name="@Html.NameFor(m => m.NoButton)" value="@Resources.No" /> 

在控制器中,只有與用戶單擊的按鈕關聯的屬性將包含一個值。其他「按鈕」屬性將爲空。所以:

// Do something if the user clicked on the Yes button 
if (model.YesButton!= null) <Do Something> 
0

我已經解決了。我不喜歡太多,但如果你有更好的解決辦法,請張貼:

查看:

<button type="submit" value="@Resources.Yes"> 
<button type="submit" value="@Resources.No"> 

和cotroller:

private enum IndexSubmitResult{Yes, No}; 

private IndexSubmitResult? GetSubmitButton(string submit) 
{ 
    if(submit == MyProject.Resources.Views.Home.Index.Yes) 
     return IndexSubmitResult.Yes; 
    else if(submit == MyProject.Resources.Views.Home.Index.No) 
     return IndexSubmitResult.No; 
    else 
     return null; 
} 

public ActionResult Index(..., string submit) 
{   
    switch (GetSubmitButton(submit)) 
    { 
     case IndexSubmitResult.Yes:  
     default: 
      ..... 
      break; 
     case IndexSubmitResult.No: 
      .... 
      break; 
    } 
} 
0

在您的程序集或其他程序集中創建資源文件。
使用字符串屬性創建viewmodel類。
像這樣使用顯示屬性。

public class ViewModelClass 
{ 
    [Display(Name = "locproperty ", ResourceType = typeof(YourResoureFile))] 
    public string locproperty { get; set; } 
} 

然後在您的視圖中使用它像這樣

<a class="btn btn-default" href="~/Controller/Action" @Html.LabelFor(m => m.locproperty) </a>