Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.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 查看mvc3中的多个IEnumerable实现_Asp.net Mvc_Asp.net Mvc 3_Asp.net Mvc 4_Mvcgrid - Fatal编程技术网

Asp.net mvc 查看mvc3中的多个IEnumerable实现

Asp.net mvc 查看mvc3中的多个IEnumerable实现,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4,mvcgrid,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 4,Mvcgrid,在我的代码中有两次 @Html.Grid(data).Columns(columns => { columns.Add(c => c.APPLICATION_NO).Titled("Application No"); }) 我不知道什么是应用程序,\u NO,所以我使用了字符串,接口只能为网格定义get 否则,如果需要为这两种类型显示不同的数据,则应考虑在IF块中使用两种视图或不同的网格声明。 我在VS上做了一个答案样本: 随信附上代码: publi

在我的代码中有两次

    @Html.Grid(data).Columns(columns =>
    {
    columns.Add(c => c.APPLICATION_NO).Titled("Application No");
    })
我不知道什么是应用程序,\u NO,所以我使用了字符串,接口只能为网格定义get

否则,如果需要为这两种类型显示不同的数据,则应考虑在IF块中使用两种视图或不同的网格声明。

我在VS上做了一个答案样本:

随信附上代码:

public interface IAPPLICATION_NO
{
    string APPLICATION_NO { get; }
}
这些是课程

现在控制器:

public interface AInterface
{
    string AProperty { get; }
}

public class AList : AInterface
{
    public string AProperty { get; set; }
}

public class BList : AInterface
{
    public string AProperty { get; set; }
}
@{IEnumerable<dynamic> data = (IEnumerable<dynamic>)ViewBag.list;} 
@Html.Grid(data).Columns(columns =>
{
columns.Add(c => c.APPLICATION_NO).Titled("Application No");
})
公共类TestController:Controller
{
公共行动结果索引()
{
var random=new random((int)DateTime.Now.Ticks);
if(random.Next()%2==0)
ViewBag.List=新列表{new AList{AProperty=“Atestvalue”};
其他的
ViewBag.List=新列表{new BList{AProperty=“Atestvalue”};
返回视图();
}
}
以及以下观点:

public class TestController : Controller
{
    public ActionResult Index()
    {
        var random = new Random((int)DateTime.Now.Ticks);

        if (random.Next() % 2 == 0)
            ViewBag.List = new List<AList> { new AList { AProperty = "Atestvalue" } };
        else
            ViewBag.List = new List<BList> { new BList { AProperty = "Atestvalue" } };

        return View();
    }
}
@{
ViewBag.Title=“Index”;
IEnumerable test=ViewBag.List;
}
指数
@foreach(测试中的var项目)
{
@项目1.财产
}
正如您所见,这解决了您的问题

不使用接口:

@{
    ViewBag.Title = "Index";
    IEnumerable<TestMvcApplication.Interfaces.AInterface> test = ViewBag.List;
}

<h2>Index</h2>

@foreach (var item in test)
{
    <div>
        @item.AProperty
    </div>
}
@{IEnumerable数据=(IEnumerable)ViewBag.list;}
@Html.Grid(数据).Columns(Columns=>
{
添加(c=>c.APPLICATION_NO),标题为(“APPLICATION NO”);
})
但是您将丢失IntelliSense完成,如果成员丢失,我认为您将收到运行时错误

我尝试了你的网格组装,但它使用c#表达式,并且与dynamic不兼容。 另一种解决方案是在控制器中使用LINQ将一个列表强制转换为另一个列表:

public interface AInterface
{
    string AProperty { get; }
}

public class AList : AInterface
{
    public string AProperty { get; set; }
}

public class BList : AInterface
{
    public string AProperty { get; set; }
}
@{IEnumerable<dynamic> data = (IEnumerable<dynamic>)ViewBag.list;} 
@Html.Grid(data).Columns(columns =>
{
columns.Add(c => c.APPLICATION_NO).Titled("Application No");
})
IEnumerable数据;
如果(某些条件)
{
data=applicationList;//applicationList的类型是IEnumerable
}
其他的
{
data=rightsList.Select(t=>newapplication{Application\u NO=t.Application\u NO});//rightsList的类型是IEnumerable
}
ViewBag.list=数据;
在视图中,您可以将发布的工作代码保留在问题的顶部。您没有多类型IEnumerable支持,因为您只使用了一种类型,但没有在这些类之间使用公共接口。我认为我们必须使用反射,但我认为编写该代码很困难

我不知道什么是应用程序,\u NO,所以我使用了字符串,接口只能为网格定义get

否则,如果需要为这两种类型显示不同的数据,则应考虑在IF块中使用两种视图或不同的网格声明。

我在VS上做了一个答案样本:

随信附上代码:

public interface IAPPLICATION_NO
{
    string APPLICATION_NO { get; }
}
这些是课程

现在控制器:

public interface AInterface
{
    string AProperty { get; }
}

public class AList : AInterface
{
    public string AProperty { get; set; }
}

public class BList : AInterface
{
    public string AProperty { get; set; }
}
@{IEnumerable<dynamic> data = (IEnumerable<dynamic>)ViewBag.list;} 
@Html.Grid(data).Columns(columns =>
{
columns.Add(c => c.APPLICATION_NO).Titled("Application No");
})
公共类TestController:Controller
{
公共行动结果索引()
{
var random=new random((int)DateTime.Now.Ticks);
if(random.Next()%2==0)
ViewBag.List=新列表{new AList{AProperty=“Atestvalue”};
其他的
ViewBag.List=新列表{new BList{AProperty=“Atestvalue”};
返回视图();
}
}
以及以下观点:

public class TestController : Controller
{
    public ActionResult Index()
    {
        var random = new Random((int)DateTime.Now.Ticks);

        if (random.Next() % 2 == 0)
            ViewBag.List = new List<AList> { new AList { AProperty = "Atestvalue" } };
        else
            ViewBag.List = new List<BList> { new BList { AProperty = "Atestvalue" } };

        return View();
    }
}
@{
ViewBag.Title=“Index”;
IEnumerable test=ViewBag.List;
}
指数
@foreach(测试中的var项目)
{
@项目1.财产
}
正如您所见,这解决了您的问题

不使用接口:

@{
    ViewBag.Title = "Index";
    IEnumerable<TestMvcApplication.Interfaces.AInterface> test = ViewBag.List;
}

<h2>Index</h2>

@foreach (var item in test)
{
    <div>
        @item.AProperty
    </div>
}
@{IEnumerable数据=(IEnumerable)ViewBag.list;}
@Html.Grid(数据).Columns(Columns=>
{
添加(c=>c.APPLICATION_NO),标题为(“APPLICATION NO”);
})
但是您将丢失IntelliSense完成,如果成员丢失,我认为您将收到运行时错误

我尝试了你的网格组装,但它使用c#表达式,并且与dynamic不兼容。 另一种解决方案是在控制器中使用LINQ将一个列表强制转换为另一个列表:

public interface AInterface
{
    string AProperty { get; }
}

public class AList : AInterface
{
    public string AProperty { get; set; }
}

public class BList : AInterface
{
    public string AProperty { get; set; }
}
@{IEnumerable<dynamic> data = (IEnumerable<dynamic>)ViewBag.list;} 
@Html.Grid(data).Columns(columns =>
{
columns.Add(c => c.APPLICATION_NO).Titled("Application No");
})
IEnumerable数据;
如果(某些条件)
{
data=applicationList;//applicationList的类型是IEnumerable
}
其他的
{
data=rightsList.Select(t=>newapplication{Application\u NO=t.Application\u NO});//rightsList的类型是IEnumerable
}
ViewBag.list=数据;
在视图中,您可以将发布的工作代码保留在问题的顶部。您没有multitype IEnumerable支持,因为您只使用了一种类型,但没有在这些类之间使用公共接口。我认为我们必须使用反射,但我认为编写该代码很困难。

为什么不呢

IEnumerable<BC.Models.Application> data;
if (some condition)
{
    data = applicationList; //applicationList's type is IEnumerable<BC.Models.Application>
}
else
{
    data = rightsList.Select(t => new Application { APPLICATION_NO = t.APPLICATION_NO }); //rightsList's type is IEnumerable<BC.Models.RIGHTS>
}

ViewBag.list = data;
我认为如果你试图在你的观点上太聪明的话,你最终会得到(最多)令人困惑的代码。用于渲染
IEnumerable
的网格不适用于渲染
IEnumerable
,除非
A:ISomeInterface
B:ISomeInterface
且网格渲染
ISomeInterface
。或者,只需将列名作为视图模型的属性传递即可。

为什么不呢

IEnumerable<BC.Models.Application> data;
if (some condition)
{
    data = applicationList; //applicationList's type is IEnumerable<BC.Models.Application>
}
else
{
    data = rightsList.Select(t => new Application { APPLICATION_NO = t.APPLICATION_NO }); //rightsList's type is IEnumerable<BC.Models.RIGHTS>
}

ViewBag.list = data;

我认为如果你试图在你的观点上太聪明的话,你最终会得到(最多)令人困惑的代码。用于渲染
IEnumerable
的网格不适用于渲染
IEnumerable
,除非
A:ISomeInterface
B:ISomeInterface
且网格渲染
ISomeInterface
。或者,只需将列名作为视图模型的属性传递。

您的问题是,您没有使用MVC中最重要的概念之一:视图模型

至于您最后的评论,您希望使用的是视图模型,即专门创建的用于向视图发送数据以显示数据的类

为此:

  • 创建一个具有
    应用程序\u NO
    的公共类(视图模型类所需)
  • 创建另一个将作为视图模型的公共类。这是一个在razor模板上根据需要对数据进行造型的类(在本例中,它将包含1中定义的类的列表)
  • 在控制器中,返回作为第二个参数传递模型的视图。也就是说,不要使用
    ViewBag
    /
    ViewData
    ,而是使用视图模型,例如
    返回视图(“ViewName”,model)
  • 在视图中使用模型:使用
    @model
    声明模型类型并在