7 messaggi dal 05 aprile 2017
Ciao a tutti,
sono alle prime esperienze in C# ed ASP.NET e sto provando ad eseguire una comando su un oggetto.
Il mio oggetto Gatepass ha una relazione 1 a molti con l'oggetto Quotation quindi vorrei avere
una lista di oggetti Quotation che hanno una relazione con Gatepass.
Allego sia il codice dove mi da errore e relativa immagine dell'errore che mi da ed anche il codice
dell'oggetto Quotation e Gatepass.

Dov'è l'errore?
Grazie a tutti
Immagine errore: http://prntscr.com/g1jg3j


 public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            GatePass gatePass = await db.GatePass.FindAsync(id);
            Quotation quotation = await db.Quotation.Where(x => x.gpas_id == id);
            
            if (gatePass == null)
            {
                return HttpNotFound();
            }
            
            ViewBag.customers_cust_id = new SelectList(db.Customers, "cust_id", "NomeCognomeEmail", gatePass.customers_cust_id);
            ViewBag.gpas_state = new SelectList(db.GpasState, "gpas_state_id", "NomeGpasStato", gatePass.gpas_state);
            ViewBag.persons_pers_code_in = new SelectList(db.Persons, "pers_code", "NomeCognome", gatePass.persons_pers_code_in);
            ViewBag.persons_pers_code_out = new SelectList(db.Persons, "pers_code", "NomeCognome", gatePass.persons_pers_code_out);
            ViewBag.vehicles_veh_id = new SelectList(db.Vehicles, "veh_id", "TargaLibretto", gatePass.vehicles_veh_id);
            return View(gatePass);
        }



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace WebApplication.Models
{
    [Table("GatePass")]
    public class GatePass
    {
        [Key]
        public int gpas_id { get; set; }

        [Display(Name = "Gate Pass State")]
        public Nullable<int> gpas_state { get; set; }
        
        [ForeignKey("gpas_state")]
        public GpasState GpasState { get; set; }

        [Display(Name = "Customers")]
        public Nullable<int> customers_cust_id { get; set; }

        [Display(Name = "Customers")]
        [ForeignKey("customers_cust_id")]
        public Customer customers { get; set; }

        [Display(Name = "Vehicles")]
        public Nullable<int> vehicles_veh_id { get; set; }

        
        [ForeignKey("vehicles_veh_id")]
        public Vehicle vehicles { get; set; }

        [Display(Name = "Person In")]
        public Nullable<int> persons_pers_code_in { get; set; }
        
        [ForeignKey("persons_pers_code_in")]
        public Person personsIn { get; set; }

        [Display(Name = "Person Out")]
        public Nullable<int> persons_pers_code_out { get; set; }

        
        [ForeignKey("persons_pers_code_out")]
        public Person personsOut { get; set; }

        [Display(Name = "Created by")]
        public string employee_open { get; set; }

        [Display(Name = "Closed by")]
        public string employee_closer { get; set; }

        [Display(Name = "Gatepass Open")]
        public DateTime gpas_in_time { get; set; }

        [Display(Name = "Gatepass Closed")]
        public DateTime gpas_out_time { get; set; }

        [Display(Name = "Remarks Note")]
        [StringLength(255, ErrorMessage = "Max 255 character")]
        public string gpas_remarks { get; set; }

        [Display(Name = "Note")]
        [StringLength(255, ErrorMessage = "Max 255 character")]
        public string gpas_note { get; set; }

        [Display(Name = "Rec Status")]
        [StringLength(8)]
        public string gpas_rec_status { get; set; }
    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace WebApplication.Models
{
    [Table("Quotation")]
    public class Quotation
    {
        [Key]
        public int quot_no { get; set; }

        [Display(Name = "Quotation Revision")]
        public Nullable<int> quot_rev { get; set; }

        
        public Nullable<int> cust_id { get; set; }
        [Display(Name = "Customers")]
        [ForeignKey("cust_id")]
        public Customer customers { get; set; }

        public Nullable<int> veh_id { get; set; }
        [Display(Name = "Vehicles")]
        [ForeignKey("veh_id")]
        public Vehicle vehicles { get; set; }

        public Nullable<int> gpas_id { get; set; }
        [Display(Name = "Gate Pass")]
        [ForeignKey("gpas_id")]
        public GatePass gatepass { get; set; }

        [Display(Name = "Quotation Date")]
        public DateTime quot_date { get; set; }

        [Display(Name = "Job Description")]
        [StringLength(512, ErrorMessage = "Troppo lungo (max 512 caratteri)")]
        public string quot_job_desc { get; set; }

        
        public Nullable<int> repairkit_rkit_no { get; set; }
        [Display(Name = "Repair Kit")]
        [ForeignKey("repairkit_rkit_no")]
        public RepairKit repairkit { get; set; }

        public Nullable<int> estim_emp_id { get; set; }
        [Display(Name = "Employe Estimator")]
        [ForeignKey("estim_emp_id")]
        public Employee employee { get; set; }

        [StringLength(8, ErrorMessage = "Troppo lungo (max 8 caratteri)")]
        public string quot_rec_status { get; set; }

        [StringLength(45, ErrorMessage = "Troppo lungo (max 45 caratteri)")]
        public string quot_mother { get; set; }

        public static implicit operator Quotation(List<Quotation> v)
        {
            throw new NotImplementedException();
        }
    }
}
244 messaggi dal 22 gennaio 2017
Contributi
await db.Quotation.FirstOrDefaultAsync(x => x.gpas_id == id);


L'istruzione await db.Quotation.Where(x => x.gpas_id == id) ha 3 problemi. Semplifico le risposte mettendo meno tecnicismi possibile, così spero di risultare più chiaro.
1) La funzione Where ritorna un IQueryable che è una "lista" di oggetti mentre ti aspetti un solo oggetto.
2) Un oggetto IQueryable non ritorna un oggetto di tipo (Task) awaitable e quindi non è possibile eseguire l'operatore await.
3) L'oggetto IQueryable viene utilizzato per "accodare" istruzioni che verranno poi convertite in SQL.
Le istruzioni che vanno ad eseguire una query sono: First, Single, Any, Count, Find e ToList.

Torna al forum | Feed RSS

ASPItalia.com non è responsabile per il contenuto dei messaggi presenti su questo servizio, non avendo nessun controllo sui messaggi postati nei propri forum, che rappresentano l'espressione del pensiero degli autori.