[英]How to access object from Inherits model in user control in ASPX markup code
我在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; }
}
我的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变量测试获得标题值?
可能可以通过添加一些getter来解决它(这样它就不会得到那些'['和']'),如下所示:
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.