2013-01-07 87 views
1

我有这样的代码:实现扩展方法

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 %>">&nbsp;</div> 
    <div class="<%: moduleScoreClass %>"><%: module.ModuleScore %>%</div> 
    <% Zinc.Web.Extensions.DataModel.Training.TrainingModuleProgressStateDataModelExtentions.GetHintText(module.UserTrainingPoints); %> //still not correct here 
+0

你向我们展示了不包含任何扩展方法的代码。你想成为一种扩展方法的方法是什么?哪种类型? –

+0

类名建议'TrainingModuleProgressStateDataModelExtentions.GetHintText'应该是一个扩展方法,但它只接受一个字符串作为其唯一参数。这看起来不正确。尝试让它不首先使用扩展方法,然后将其转换为扩展方法。 – hvd

回答

4

由于UserTrainingPointsIEnumerable<UserTrainingPointsDataModel>我会想你的扩展方法的签名应该是

public static string GetHintText(this IEnumerable<UserTrainingPointsDataModel> points) 
{ 
    string aString; 
    //must still make up astring here 
    return aString; 
} 

然后,你可以这样调用

module.UserTrainingPoints.GetHintText(); 
+0

谢谢,但我应该在module.UserTrainingPoints.GetHintText()中传递一些信息并不确定是什么? –

+0

从技术上讲,你正将'module.UserTrainingPoints'传递给方法。 – juharr

+0

在视图中得到一个错误,指出Zinc.Models.Training.TrainingModuleProgressStateDataModelResults不包含GetHintText的定义?我有这个在我看来:<%@ using Zinc.Web.Extensions.DataModel.Training%>? –

0

访问扩展方法做同样的方式,你通常会做。 但是,您应该注意您使用@using关键字引用了扩展名添加到的名称空间。

+0

谢谢,我应该将这添加到我的视图吗? <%@ using Zinc.Web.Extensions.DataModel.Training%> –