使用在显示模板中隐式转换为声明的@model的模型

时间:2015-10-18 09:00:10

标签: c# asp.net-mvc

我有以下课程:

public class MyClass
{
    public int Number {get; set;}
}

和扩展版本:

public class ExtendedMyClass
{
   public MyClass Data {get; set;}
   public ExtendedMyClass(MyClass data)
   {
       Data = data;
   }
   public static implicit operator ExtendedMyClass(MyClass data) => return new ExtendedMyClass(data);
}

在显示模板中,我有@model ExtendedMyClass

我希望能够做到以下几点:

foreach(var item in AllData)/* AllData is a list that contains a mix of MyClass and ExtendedMyClass*/
{
    @Html.DisplayFor(m => item)
}

但是我收到以下错误:

  

传入字典的模型项的类型是' MyClass',但是   这本词典需要一个类型为' ExtendedMyClass'

的模型项

有没有办法在不滚动自定义IModelBinder实现的情况下工作?

1 个答案:

答案 0 :(得分:1)

运行时不知道它应该使用哪种类型。它只看到原始类型。只需使用显式转换:

func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

    if endDragOffset > 0 {
        // moving forward, get next
        if(scrollView.contentOffset.x > mainScrollView!.frame.size.width) {
            prevWebView = currWebView // get the content of the next one
            currWebView = nextWebView

            // set back the position
            prevWebView.frame.origin.x = 0
            currWebView.frame.origin.x = 320
            nextWebView.frame.origin.x = 640

            var theNext = getNext(nextVolume, theChapter: nextChapter)
            nextVolume = theNext["volume"]!
            nextChapter = theNext["chapter"]!
            nextChapterArr = getLection(nextVolume, theChapter: nextChapter)
            loadPage(nextVolume, theChapter: nextChapter, chapterArr: nextChapterArr, onPage: 2)

            print(currWebView.frame.origin.x) // got 640

            currWebView.frame.origin.x = 320
            print(currWebView.frame.origin.x) // still got 640
        }

        // moving backward, get previous
        if(scrollView.contentOffset.x < mainScrollView!.frame.size.width) {
            //.........
        }