【问题标题】:ASP.NET MVC Submit button localizationASP.NET MVC 提交按钮本地化
【发布时间】:2013-10-09 06:11:18
【问题描述】:

我已经本地化了带有视图的 ASP.NET MVC 应用程序:

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

和控制器:

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,他们使用属性,但我不能使用它,因为本地化不是恒定的。

【问题讨论】:

    标签: asp.net-mvc localization form-submit


    【解决方案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>
    

    【讨论】:

      【解决方案2】:

      我已经解决了。我不太喜欢它,但是如果您有更好的解决方案,请发布:

      查看:

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

      和控制器:

      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;
          }
      }
      

      【讨论】:

        【解决方案3】:

        在您的程序集或其他程序集中创建资源文件。
        创建一个带有字符串属性的视图模型类。
        像这样使用显示属性。

        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>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-08-02
          • 2011-07-11
          • 2014-07-15
          • 1970-01-01
          • 2012-05-10
          • 2013-05-02
          • 1970-01-01
          相关资源
          最近更新 更多