将var计算结果传递给视图 [英] Passing a var calculated result to view

查看:80
本文介绍了将var计算结果传递给视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是asp.net MVC的新手所以请不要评价我,



我有型号



 命名空间 WebApplication1.Models 
{
公开 productDB
{
public int Producttype { get ; set ; }
public string Productquantity { get ; set ; }





i在我的控制器中有这个计算:



  public  ActionResult prog()
{
var TypeQty = db.productDB.GroupBy(fu = > fu.Producttype)
。选择(g = > new {Name = g.Key,Count = g.Count()});
return TypeQty;
// 此处遇到问题以及无法正确转换类型

}





如何将计算结果传递给我的视图?谢谢你的帮助!



原始代码中对象的名称不是英文,由于快速重写为英文可能存在不一致

解决方案

你应该创建一个具有Name和Count属性的具体类,并创建一个列表



  var  TypeQty = db.productDB.GroupBy(fu = >  fu.Producttype)
.Select( g = > new YourClassNameHere {Name = g.Key,Count = g.Count()}) ;





您的型号现在将是



@model IEnumerable< yourclassnamehere> ;


Hello, i am new to asp.net MVC so please dont judge me,

I have a model

namespace WebApplication1.Models
{
    public class productDB
    {
        public int Producttype { get; set; }
        public string Productquantity { get; set; }



i have this calculation in my controller:

public ActionResult prog()
        {
            var TypeQty = db.productDB.GroupBy(fu => fu.Producttype)
            .Select(g => new { Name = g.Key, Count = g.Count() });
            return TypeQty;
//running into a problem here aswell "Cannot imlicitly convert type"

        }



How do i pass the result of calculation to my view? thanks for your help!

the names of object in my original code is not in english, there might be inconsistencies due to quick rewrite to english

解决方案

You should create a concrete class with a Name and Count property and create a list of those

var TypeQty = db.productDB.GroupBy(fu => fu.Producttype)
            .Select(g => new YourClassNameHere{ Name = g.Key, Count = g.Count() });



Your model will now be

@model IEnumerable<yourclassnamehere>


这篇关于将var计算结果传递给视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆