我有这段代码:
namespace Zinc.Web.Extensions.DataModel.Training
{
public static class TrainingModuleProgressStateDataModelExtentions
{
public static string GetHintText(String aString)
{
//must still make up astring here
return aString;
}
}
}
namespace Zinc.DataModels.Training
{
public class TrainingModuleProgressStateDataModel
{
public IEnumerable<UserTrainingPointsDataModel> UserTrainingPoints { get; set; }
}
}
public class UserTrainingPointsDataModel
{
public virtual int InteractionType { get; set; }
public virtual int Points { get; set; }
public virtual string Name { get; set; }
public virtual string IncentiveTrainingModuleOptionName { get; set; }
}
在我的存储库中,我添加到UserTrainingPoints:
string RawPoints = row["RawPoints"].ToString();
string[] rawPoints = RawPoints.Split(new char[] { '|' });
List<UserTrainingPointsDataModel> points = new List<UserTrainingPointsDataModel>();
foreach (var RawPoint in rawPoints)
{
string[] entry = RawPoint.Split(new char[] { ',' });
var point = new UserTrainingPointsDataModel();
point.Name = entry[0];
point.Points = Convert.ToInt32(entry[1]);
point.InteractionType = Convert.ToInt32(entry[2]);
point.IncentiveTrainingModuleOptionName = entry[3];
points.Add(point);
}
trainingModuleProgressState.UserTrainingPoints = points;
data.Add(trainingModuleProgressState);
在我的视图中,我需要调用扩展方法,该方法将使用UserTrainingPoints中的值来组成一个字符串,然后我将在工具提示中显示该字符串。
我的问题是我如何实现扩展方法,以便我可以在我的视图中调用它?
我的观看代码:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Zinc.Models.Training.TrainingModuleProgressStateDataModelResults>" %>
<%@ Import namespace="Zinc.Web.Extensions.DataModel.Training" %> //added this
<% if (module.HasAssessment)
{ %>
<div class="<%: moduleStateClass %>"> </div>
<div class="<%: moduleScoreClass %>"><%: module.ModuleScore %>%</div>
<% Zinc.Web.Extensions.DataModel.Training.TrainingModuleProgressStateDataModelExtentions.GetHintText(module.UserTrainingPoints); %> //still not correct here
答案 0 :(得分:4)
由于UserTrainingPoints
是IEnumerable<UserTrainingPointsDataModel>
,我认为你的扩展方法签名应该是
public static string GetHintText(this IEnumerable<UserTrainingPointsDataModel> points)
{
string aString;
//must still make up astring here
return aString;
}
然后你就可以这样称呼它
module.UserTrainingPoints.GetHintText();
答案 1 :(得分:0)
访问扩展方法的方法与通常的方法相同。 但是,您应该注意使用@using关键字引用添加扩展名的命名空间。