簡體   English   中英

無法更新ICollection屬性

[英]Can’t update ICollection property

問題是當我嘗試同時更新主表和明細表時。
當調用“后編輯任務”時,“詳細信息”對象不會出現。

編輯視圖正確顯示所有詳細信息行,但是在調試編輯POST時,Casas為空

楷模

 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

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);
}

查看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>

當您在Razor中使用for循環而不是foreach時,使用默認的asp-for TagHelpers時,屬性名稱無法正確顯示。

您可以按照以下方式更正示例,更改剃刀形式的輸入:

從:

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

至:

<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