2014-09-06 99 views
0

我在Web窗體項目中使用用戶控件。這是一個遺留項目,但我們從ASP.NET MVC中獲得了很多靈感,並將其納入了我們的項目。如何從ASPX標記代碼中的用戶控件Inherits模型中訪問對象

我嵌入了一個用戶控件,而不是舊的「usercontrol-can-do-everything」,我們更多地使用它們作爲partials並給它們一個viewmodel。

但是,我在代碼中訪問我的「viewmodel」中的值時出現問題。讓我展示一些代碼,我會解釋問題。

控制定義

<%@ Control Language="vb" AutoEventWireup="false" Inherits="Saxo.Websites.Shop.Web.PartialControl`1[[Saxo.Websites.Shop.Models.Recommendations.RecommendationsViewModel]]" %> 

我的 「視圖模式」

public class RecommendationsViewModel 
    { 
     public Dictionary<string, Placement> Placements { get; set; } 

     public RecommendationsViewModel() 
     { 
      this.Placements = new Dictionary<string, Placement>(); 
     } 
    } 

    public class Placement 
    { 
     public Placement(string divid, string title):this(divid,title,6) 
     { 

     } 

     public Placement(string divid, string title, int count) 
     { 
      this.DivId = divid; 
      this.Title = title; 
      this.Count = count; 
     } 
     public string DivId { get; set; } 
     public string Title { get; set; } 
     public int Count { get; set; } 
    } 

我partialcontrol文件:

public class PartialControl<T> : UserControl 
    { 
     public T Model { get; set; } 

     public static string RenderPartial(string path, object model) 
     { 
      var control = new UserControl(); 
      try 
      { 
       control = (UserControl)control.LoadControl(path); 
      } 
      catch (Exception ex) 
      { 
       throw new ApplicationException("Error loading partial control: " + path, ex); 
      }    

      var prop = control.GetType().GetProperty("Model"); 
      if (prop == null) 
      { 
       throw new ApplicationException(control.GetType().FullName + " does not implement Property 'Model'"); 
      } 


      try 
      { 
       prop.SetValue(control, model, null); 
      } 
      catch (Exception) 
      { 
       throw new ApplicationException("Error setting model on : " + control.GetType().FullName); 
      } 


      using (var sw = new StringWriter()) 
      { 
       using (var hw = new HtmlTextWriter(sw)) 
       { 
        try 
        { 
         control.RenderControl(hw); 

         return sw.ToString(); 

        } 
        catch (Exception ex) 
        { 
         throw new ApplicationException("Error Rendering Partial Control: " + control.GetType().FullName, ex); 

        } 
       } 
      }    
     } 
    } 

現在,我要做到以下幾點在我的標記:像<%<%#

<script charset="utf-8" type="text/javascript"> 
var test = '<%= Model.Placements["key_in_my_dictionary"].Title %>'; 
</script> 

我也試過變種。

不過,我得到這個錯誤:

System.Web.HttpCompileException: D:\Git\Saxo\Saxo.Websites.Base\src\Saxo.Website.Base\Views\Recommendations\_PageRecommendations.ascx(38): error BC30203: Identifier expected. 

,但我可以做成功了以下內容:

<script charset="utf-8" type="text/javascript"> 
    var test = '<%= Model.Placements.Count %>'; 
    </script> 

因此,我的問題是:我怎麼寫我的標記,所以我的JS變量測試獲得標題值?

回答

2

大概可以繞通過添加一些吸氣工作(所以它不會讓那些「[」和「]」)如下:

<script charset="utf-8" type="text/javascript"> 
    var test = '<%= Model.GetPlacement("name") %>'; 
</script> 

public class RecommendationsViewModel 
{ 
    // ... 
    public Placement GetPlacement(string name) { return Placements[name]; } 
    // ... 
} 
在ASCX

後來

或者可能是下面將在ASCX幫助:

<script runat=server> 
    Response.Write("<script charset=\"utf-8\" type=\"text/javascript\">"); 
    Response.Write("var test = " + Model.Placements["key_in_my_dictionary"].Title); 
    Response.Write("<\/script>"); 
</script> 
+0

這將工作,唯一的問題是它的實現方式,是不存在的代碼BEH ind在用戶控件中。事實證明,錯誤是由我的前端是VB.NET而不是C#(wtf?:D)造成的。但我會將您的答案標記爲答案,因爲當人們通過Google找到答案時,這是最好的方式。 – 2014-09-07 16:36:46

相關問題