2011-03-17 40 views
0

在課堂ModelError的構造均在控制器如何在if語句中顯示信息?

public ActionResult ModeError() 
     { 
      return View("ModeError", new Models.ModeErrorInfo(this.Url.RequestContext.RouteData.Values)); 
     } 

良好 鑑於

<% if (this.Model.SPECIALTY == null || string.IsNullOrEmpty(this.Model.SPECIALTY)) 
     { 
      Html.DisplayText(String.Format("{0} {1} is missing", Model.ParametrType, Model.Speciality)); 
     } %> 
    <% else 
     { 
      Html.DisplayText(String.Format("{0} {1} {2}", Model.ParametrType, Model.Speciality, Model.SPECIALTY)); 
     } %> 

public class ModeErrorInfo 
    { 

     #region Constants 

     public readonly string Session = "SESS"; 
     public readonly string Mode = "MODE"; 
     public readonly string App = "APP"; 
     public readonly string Usertype = "USERTYPE"; 
     public readonly string Speciality = "SPECIALTY"; 
     public readonly string ParametrType = "URL"; 

     #endregion 

     #region Public Properties 

     public string SESS { get; set; } 
     public string APP { get; set; } 
     public string USERTYPE { get; set; } 
     public string SPECIALTY { get; set; } 
     public string MODE { get; set; } 

     #endregion 

     #region Constructors 

     public ModeErrorInfo(RouteValueDictionary values) 
     { 
      if (values != null) 
      { 
       this.APP = (string)values[this.App]; 
       this.SESS = (string)values[this.Session]; 
       this.SPECIALTY = (string)values[this.Speciality]; 
       this.USERTYPE = (string)values[this.Usertype]; 
       this.MODE = (string)values[this.Mode]; 
      } 
     } 

     #endregion 

    } 

但我給空視圖下執行後!我如何顯示這些信息?

+0

你在問什麼?你看到什麼問題? – 2011-03-17 17:37:00

+0

執行後不是信息。 – isxaker 2011-03-17 17:37:57

回答

0

看起來你沒有正確使用Html.DisplayText()。看看documentationexample。這個函數不僅僅是把東西渲染到頁面上,而是以一種特殊的方式運行。

爲了測試,爲什麼不直接刪除對Html.DisplayText()的調用,而只是使用<%= Model.ParameterType %>寫出內容?這可以讓你知道你的模型是否包含有效的信息。