skip to Main Content

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 class="bg-transparent border-0" 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 class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Obriši</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
            <form action="/MIDKalorimetar/Delete" method="post" id="forma">
            
             
           
            <input asp-for="Id" hidden/>
            <div class="d-flex mb-2">
            <div class="d-table "><label class="d-table-cell align-middle" style="font-weight:bold">Vrijednost karakteristike :</label></div>
            <p>@Model.VrijednostKarakteristike</p>
            </div>
            <input asp-for="Id2" hidden/>
            <div class="d-flex mb-2">
            <div class="d-table "><label class="d-table-cell align-middle" style="font-weight:bold">Vrijednost karakteristike :</label></div>
            <p>@Model.VrijednostKarakteristike</p>
            </div>
            <input asp-for="Id3" hidden/>
            <div class="d-flex mb-2">
            <div class="d-table "><label class="d-table-cell align-middle" style="font-weight:bold">Vrijednost karakteristike :</label></div>
            <p>@Model.VrijednostKarakteristike</p>
            </div>
            <input asp-for="Id4" hidden/>
            <div class="d-flex mb-2">
            <div class="d-table "><label class="d-table-cell align-middle" style="font-weight:bold">Vrijednost karakteristike :</label></div>
            <p>@Model.VrijednostKarakteristike</p>
            </div>
            <input asp-for="Id5" hidden/>
            <div class="d-flex mb-2">
            <div class="d-table "><label class="d-table-cell align-middle" style="font-weight:bold">Vrijednost karakteristike :</label></div>
            <p>@Model.VrijednostKarakteristike</p>
            </div>
   
            </form>
            </div>
            <div class="modal-footer">
                <button type="submit" class="btn btn-primary red" form="forma" data-save="modal">Obriši</button>
                <a href="/MIDKalorimetar/Index1" class="btn btn-warning">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.

3

Answers


  1. 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.

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
  3. i have the same issue a time a go i solved with this

    [ForeignKey("ClassId")] public MySourceCustomClass myCustomClass { get; set; }

    for ex

    [ForeignKey("CityId")] public Cityes MyCity {get; set;}

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search