简体   繁体   English

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

[英]MVC4 Cannot implicitly convert type model in controller

I am trying to get some information from my database through a stored procedure and then transfer it from my controller to my view: 我试图通过存储过程从数据库中获取一些信息,然后将其从控制器传输到视图:

 [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);
    }

But during runtime, I get the next error 但是在运行时,出现下一个错误
Cannot implicitly convert type'System.Data.Objects.ObjectResult' to 'DataRebuilder.Models.GetIndividualSensorRecord_Result'. 无法将类型'System.Data.Objects.ObjectResult'隐式转换为'DataRebuilder.Models.GetIndividualSensorRecord_Result'。

How can i correctly achieve this? 我怎样才能正确地做到这一点?

Also if i place: 另外,如果我放置:

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

This is my model: 这是我的模型:

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; }
}}

This is in my 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 CAN transfer it, but how would I be able to explore the variable Records in the view? 我可以传输它,但是如何在视图中浏览变量Records? IE my model has 10 different strings that correspond. IE浏览器我的模型有10个不同的字符串对应。

Appreciate the help. 感谢帮助。

I believe the issue is that GetIndividualSensorRecord() returns an ObjectResult but you are trying to set it to the complex type GetIndividualSensorRecord_Result. 我认为问题在于GetIndividualSensorRecord()返回一个ObjectResult,但是您试图将其设置为复杂类型GetIndividualSensorRecord_Result。 Try doing a .FirstOrDefault() 尝试做一个.FirstOrDefault()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM