asp.net 表达式在"Include“操作中无效,因为它不表示属性访问:

pgx2nnw8  于 2023-03-04  发布在  .NET
关注(0)|答案(3)|浏览(168)

控制器:

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

删除模态:

@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>

因此,我尝试将此ID显示到模态,当我调试时,我可以看到ID被传递到控制器,但它不会打开删除模态,并出现以下错误:
无效操作异常:表达式"(d. Id == __modelVM_Id2_1)"在"Include"操作中无效,因为它不表示属性访问:"t =〉t.MyProperty'。若要以在派生类型上声明的导航为目标,请使用强制转换('t =〉((Derived)t). MyProperty')或" as "运算符('t =〉(t as Derived). MyProperty')。集合导航访问可以通过组合Where、OrderBy(Descending)、ThenBy(Descending)、Skip或Take操作来筛选。有关包括相关数据的详细信息,请参见http://go.microsoft.com/fwlink/?LinkID=746393

xmakbtuz

xmakbtuz1#

Include不是这样工作的,它是用来加载related data的,related data是用导航属性表示的。
如果你想访问由一个传递的id表示的实体,你可以把这些id聚合到集合中,然后使用Contains

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

虽然FirstOrDefaultAsync在这种情况下对我来说没有多大意义。

4xrmg8kj

4xrmg8kj2#

Hello在使用Include CurrentClass.Include(x => x.YourClass)时在表达式中使用整个类。并向当前类添加属性,如下所示:

public YourClass YourClass{ get: set; }

如果您将YourClassId作为YourClass的外键,则可以访问YourClass true CurrentClass.YourClass

5q4ezhmt

5q4ezhmt3#

我有同样的问题,一次去我解决了这个问题
[外键("类ID")]公共的我的源定制类设置;}
例如
[外键("城市ID")]公共城市我的城市{get;设置;}

相关问题