MVC4无法在控制器中隐式转换类型模型

时间:2015-07-17 16:52:17

标签: c# asp.net-mvc-4 model controller type-conversion

我试图通过存储过程从我的数据库中获取一些信息,然后将其从我的控制器传输到我的视图:

 [HttpPost]
    public ActionResult MostrarRecord(String Fecha)
    {
        ViewBag.Message = "Siga los pasos para completar el proceso...";
        retail2Entities cl = new retail2Entities();
        DateTime fechaBase = Convert.ToDateTime(Fecha);
        GetIndividualSensorRecord_Result Records = cl.GetIndividualSensorRecord(ViewBag.IDSENSOR, ViewBag.TIPOSENSOR, fechaBase);


        return View(Records);
    }

但是在运行时,我得到了下一个错误 无法将 type'System.Data.Objects.ObjectResult'隐式转换为'DataRebuilder.Models.GetIndividualSensorRecord_Result'。

我怎样才能正确实现这一目标?

如果我放置:

  

var Records = cl.GetIndividualSensorRecord(ViewBag.IDSENSOR,   ViewBag.TIPOSENSOR,fechaBase);

这是我的模特:

namespace DataRebuilder.Models
{
using System;

public partial class GetIndividualSensorRecord_Result
{
    public System.DateTime date { get; set; }
    public decimal idSensor { get; set; }
    public bool enter { get; set; }
    public Nullable<decimal> C01 { get; set; }
    public Nullable<decimal> C02 { get; set; }
    public Nullable<decimal> C03 { get; set; }
    public Nullable<decimal> C04 { get; set; }
    public Nullable<decimal> C05 { get; set; }
    public Nullable<decimal> C06 { get; set; }
    public Nullable<decimal> C07 { get; set; }
    public Nullable<decimal> C08 { get; set; }
    public Nullable<decimal> C09 { get; set; }
    public Nullable<decimal> C10 { get; set; }
    public Nullable<decimal> C11 { get; set; }
    public Nullable<decimal> C12 { get; set; }
    public Nullable<decimal> C13 { get; set; }
    public Nullable<decimal> C14 { get; set; }
    public Nullable<decimal> C15 { get; set; }
    public Nullable<decimal> C16 { get; set; }
    public Nullable<decimal> C17 { get; set; }
    public Nullable<decimal> C18 { get; set; }
    public Nullable<decimal> C19 { get; set; }
    public Nullable<decimal> C20 { get; set; }
    public Nullable<decimal> C21 { get; set; }
    public Nullable<decimal> C22 { get; set; }
    public Nullable<decimal> C23 { get; set; }
    public Nullable<decimal> C00 { get; set; }
}}

这是我的context.cs

    public virtual ObjectResult<GetIndividualSensorRecord_Result> GetIndividualSensorRecord(Nullable<int> idsensor, Nullable<int> entrada, Nullable<System.DateTime> fech)
    {
        var idsensorParameter = idsensor.HasValue ?
            new ObjectParameter("idsensor", idsensor) :
            new ObjectParameter("idsensor", typeof(int));

        var entradaParameter = entrada.HasValue ?
            new ObjectParameter("entrada", entrada) :
            new ObjectParameter("entrada", typeof(int));

        var fechParameter = fech.HasValue ?
            new ObjectParameter("fech", fech) :
            new ObjectParameter("fech", typeof(System.DateTime));

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<GetIndividualSensorRecord_Result>("GetIndividualSensorRecord", idsensorParameter, entradaParameter, fechParameter);
    }

我可以转移它,但我怎样才能在视图中探索变量记录? I.E.我的模型有10个不同的字符串对应。

感谢帮助。

1 个答案:

答案 0 :(得分:1)

我认为问题是GetIndividualSensorRecord()返回一个ObjectResult但你试图将它设置为复杂类型GetIndividualSensorRecord_Result。尝试做一个.FirstOrDefault()