简体   繁体   English

无法更新ICollection属性

[英]Can’t update ICollection property

The problem is when I try to update Master and Details Tables at the same time. 问题是当我尝试同时更新主表和明细表时。
When call Post Edit Task the Details objects don´t appear. 当调用“后编辑任务”时,“详细信息”对象不会出现。

The Edit View displays all Details rows correctly, but while debugging the Edit POST, Casas is empty 编辑视图正确显示所有详细信息行,但是在调试编辑POST时,Casas为空

MODELS 楷模

 public partial class Modelo : IValidatableObject {
     public Modelo()    
     {
         Casas = new HashSet<Casa>();
     }

     public int Modeloid { get; set; }
     public string Modelo1 { get; set; }
     public virtual ICollection<Casa> Casas { get; set; }//Don’t work to update
}

public partial class Casa   //  DETAIL TABLE
{
    public int Casaid { get; set; }
    public int Modeloid { get; set; }  // FK to Modelo
    public string Casa1 { get; set; }
    public virtual Modelo Modelo { get; set; }
}

CONTROLLER CONTROLLER

public class ModelosController : Controller

. . . . . . . . . 
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id,  Modelo modelo)
{
   if (id != modelo.Modeloid)
   {
       return NotFound();
   }
   if (ModelState.IsValid)
   {
   //   Here modelo.Modelo1 has current modified value 
   //   but modelo.Casas.Count == 0
        _context.Update(modelo);  
        await _context.SaveChangesAsync();
   }
}

// GET: Modelos/Edit
public async Task<IActionResult> Edit(int? id)
{
    if (id == null)
    {
        return NotFound();
    }
    var modelo = await _context.Modelo
                       .AsNoTracking() 
                       .Include(m => m.Fotomodelos)
                       .Include(m => m.Casas)
                       .SingleOrDefaultAsync(m => m.Modeloid == id);
    if (modelo == null)
    {
        return NotFound();
    }
    return View(modelo);
}

View EDIT.CSHTML 查看EDIT.CSHTML

@using System.IO
@model Disponibilidad.Models.Modelo


<form asp-action="Edit">
  <div class="form-horizontal">
  <hr />
    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
      <input type="hidden" asp-for="Modeloid" />
      <div class="form-group">
        <label asp-for="Modelo1" class="col-md-2 control-label"></label>
        <div class="col-md-10">
           <input asp-for="Modelo1" class="form-control" />
           <span asp-validation-for="Modelo1" class="text-danger"></span>
        </div>
      </div>
      @{
       for (int i = 0; i < Model.Casas.Count; i++)
       {
            <input type="hidden" asp-for="@Model.Casas.ElementAt(i).Modeloid"
                             value="@Model.Modeloid" />
            <input type="hidden" asp-for="@Model.Casas.ElementAt(i).Casaid" />
        <div class="form-group">
         <label asp-for="@Model.Casas.ElementAt(i).Casa1" 
            class="col-md-2 control-label"></label>
         <div class="col-md-10">
            <input asp-for="@Model.Casas.ElementAt(i).Casa1"      
               class="form-control" />    <!--  DISPLAY OK Detail rows -->  
            <span asp-validation-for="@Model.Casas.ElementAt(i).Casa1" 
              class="text-danger"></span>
         </div>
        </div>
       }
       }

       <div class="btn-group">
         <button type="submit" class="btn btn-danger">Save</button>
       </div>
    </div>
</form>

When you use a for cycle instead of foreach in Razor, the name of the properties doesn't get rendered correctly when using the default asp-for TagHelpers. 当您在Razor中使用for循环而不是foreach时,使用默认的asp-for TagHelpers时,属性名称无法正确显示。

You can correct your example changing your razor form inputs as follow: 您可以按照以下方式更正示例,更改剃刀形式的输入:

From: 从:

<input type="hidden" asp-for="@Model.Casas.ElementAt(i).Casaid" />

To: 至:

<input type="hidden" name="modelo.Casas[@i].Casaid" value="@Model.Casas.ElementAt(i).Casaid" />

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

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