如何在ASPX标记代码中的用户控件中从Inherits模型访问对象

时间:2014-09-06 10:51:55

标签: c# asp.net webforms asp.net-3.5

我在Web窗体项目中使用用户控件。这是一个遗留项目,但我们从ASP.NET MVC中获得了很多灵感,并将其包含在我们的项目中。

我嵌入了一个用户控件,但是我们更多地将它们用作部分并给它们一个视图模型,而不是旧的“usercontrol-can-do-everything”。

但是,我在代码中从“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; }
    }

我的部分控制文件:

 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变量测试获得标题值?

1 个答案:

答案 0 :(得分:2)

可能可以通过添加一些getter来解决这个问题(因此它不会得到如下所示的那些&#39; [&#39;和&#39;]&#39;),如下所示:

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

以及后来的ascx:

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

或者以下内容可能有助于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>