Home > Software design >  The expression is invalid inside an 'Include' operation, since it does not represent a pro
The expression is invalid inside an 'Include' operation, since it does not represent a pro

Time:03-11

Controller:

[HttpGet]
    [Route("MIDKalorimetar/Delete/{Id}/{Id2}/{Id3}/{Id4}/{Id5}")]
    public async Task<IActionResult> Delete(DeleteKalorimetarVM modelVM)
    {
        var model = await _db.ParametriMjerila.Where(x => x.Id == modelVM.Id).Include(d => d.Id == modelVM.Id2).Include(x=>x.Id == modelVM.Id3).Include(x => x.Id == modelVM.Id4).Include(x => x.Id == modelVM.Id5).FirstOrDefaultAsync();
            
        return PartialView("Delete", model);
    }

[HttpPost]
public async Task<IActionResult> Delete(ParametarMjerila parametrniMjerila)
{
    var model = await _db.ParametriMjerila.Where(x => x.Id == parametrniMjerila.Id).FirstOrDefaultAsync();
     _db.ParametriMjerila.Remove(model);
     _db.SaveChanges();

    return RedirectToAction("Index", model);
}

Button in Index for modal:

<button  style=" background: transparent; border: 0; border: 0 !important; " data-toggle="ajax-modal" data-url="@Url.Action($"Delete/{@Model.Qi.ElementAtOrDefault(x)?.Id}/{@Model.Qp.ElementAtOrDefault(x)?.Id}/{@Model.Qs.ElementAtOrDefault(x)?.Id}/{@Model.R.ElementAtOrDefault(x)?.Id}/{@Model.SP.ElementAtOrDefault(x)?.Id}")">

Delete modal:

 @model VerifikacijaMjerila.ViewModels.MIDKalorimetar.DeleteKalorimetarVM

<div  id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div >
        <div >
            <div >
                <h5  id="exampleModalLabel">Obriši</h5>
                <button type="button"  data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div >
            <form action="/MIDKalorimetar/Delete" method="post" id="forma">
            
             
           
            <input asp-for="Id" hidden/>
            <div >
            <div ><label  style="font-weight:bold">Vrijednost karakteristike :</label></div>
            <p>@Model.VrijednostKarakteristike</p>
            </div>
            <input asp-for="Id2" hidden/>
            <div >
            <div ><label  style="font-weight:bold">Vrijednost karakteristike :</label></div>
            <p>@Model.VrijednostKarakteristike</p>
            </div>
            <input asp-for="Id3" hidden/>
            <div >
            <div ><label  style="font-weight:bold">Vrijednost karakteristike :</label></div>
            <p>@Model.VrijednostKarakteristike</p>
            </div>
            <input asp-for="Id4" hidden/>
            <div >
            <div ><label  style="font-weight:bold">Vrijednost karakteristike :</label></div>
            <p>@Model.VrijednostKarakteristike</p>
            </div>
            <input asp-for="Id5" hidden/>
            <div >
            <div ><label  style="font-weight:bold">Vrijednost karakteristike :</label></div>
            <p>@Model.VrijednostKarakteristike</p>
            </div>
   
            </form>
            </div>
            <div >
                <button type="submit"  form="forma" data-save="modal">Obriši</button>
                <a href="/MIDKalorimetar/Index1" >Nazad</a>
            </div>
           
        </div>
    </div>
</div>

So am trying to display this IDs to modal, when I debbug I can see that IDs are passed to controller but it wont open delete modal and the following error appears:

InvalidOperationException: The expression '(d.Id == __modelVM_Id2_1)' is invalid inside an 'Include' operation, since it does not represent a property access: 't => t.MyProperty'. To target navigations declared on derived types, use casting ('t => ((Derived)t).MyProperty') or the 'as' operator ('t => (t as Derived).MyProperty'). Collection navigation access can be filtered by composing Where, OrderBy(Descending), ThenBy(Descending), Skip or Take operations. For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393.

CodePudding user response:

Include does not work like that. It is used to load related data which is represented with navigation properties.

If you want to access entities represented by one of the passed ids you can aggregate those ids into collection and use Contains:

var ids = new [] { modelVM.Id, modelVM.Id2, ...};
var model = await _db.ParametriMjerila
    .Where(x => ids.Contains(x.Id))
    .FirstOrDefaultAsync();

Though FirstOrDefaultAsync does not make much sense to me in this case.

CodePudding user response:

Hello while using Include CurrentClass.Include(x => x.YourClass) use the whole class in the expression. And add property to the current class like this:

public YourClass YourClass{ get: set; }

If you have YourClassId as a ForeignKey to YourClass You will have access to YourClass true CurrentClass.YourClass

  • Related