Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 在ASP.NET mvc4中使用接口_Asp.net Mvc 4_Razor_Interface - Fatal编程技术网

Asp.net mvc 4 在ASP.NET mvc4中使用接口

Asp.net mvc 4 在ASP.NET mvc4中使用接口,asp.net-mvc-4,razor,interface,Asp.net Mvc 4,Razor,Interface,我正在尝试实现一个具有getFirstStudent方法的接口。我的想法是使此方法适用于所有使用我的局部视图的viewmodels。通过这种方式,我将解决将多个视图模型(理论上不可能)传递到局部视图的问题 接口 public Interface IMiniView { string GetFirstStudentId(); } FirstViewmodel: namespace XXX.ViewModel { public class StudentsViewModel

我正在尝试实现一个具有getFirstStudent方法的接口。我的想法是使此方法适用于所有使用我的局部视图的viewmodels。通过这种方式,我将解决将多个视图模型(理论上不可能)传递到局部视图的问题

接口

public Interface IMiniView 
{
       string GetFirstStudentId();
}
FirstViewmodel:

namespace XXX.ViewModel
{
    public class StudentsViewModel: IMiniView 
    {
        public IEnumerable<StudentInfoViewModel> StudentList { get; set; }

        public string GetFirstStudentId()
        {
            return = ???????
        }
    }
}
现在我想在GetFirstStudent方法中访问它。请注意,StudentInfo视图模型有一个名为StudentID的字符串,这有帮助吗

namespace CivicaEducation.Business.Ces.ViewModel
{
    public class StudentsViewModel: IMiniView 
    {
        public IEnumerable<StudentInfoViewModel> StudentList { get; set; }

        private StudentInfoViewModel FirstStudent 
        {
            get 
            {
                if (StudentList == null || StudentList.Count() == 0) {
                    return null;
                }

                return StudentList.FirstOrDefault();
            }
        }

        public string GetFirstStudentId()
        {
            return FirstStudent == null : string.Empty ? FirstStudent.Id;
        }

                    // you can add more methods / properties to you interface like this
        public string GetFirstStudentName()
        {
            return FirstStudent == null : string.Empty ? FirstStudent.Name;
        }
    }
}
namespace CivicaEducation.Business.Ces.ViewModel
{
公共类学生视图模型:IMiniView
{
公共IEnumerable学生列表{get;set;}
私人学生信息视图模型FirstStudent
{
得到
{
if(StudentList==null | | StudentList.Count()==0){
返回null;
}
return StudentList.FirstOrDefault();
}
}
公共字符串GetFirstStudentId()
{
return firstststudent==null:string.Empty?firstststudent.Id;
}
//您可以像这样向接口添加更多方法/属性
公共字符串GetFirstStudentName()
{
return firstststudent==null:string.Empty?firstststudent.Name;
}
}
}
您可以使用这个

return StudentList.First().SutdentId;

嗨,凯恩,谢谢你的回复。。但编译器不喜欢说viewmodel不包含对FirstorDefault或CountAdd的任何定义,而是在对
System.Linq
命名空间的引用中添加
return StudentList.First().SutdentId;