Hi
I'm trying to save data updated on the partial view called in Edit action.
I have these models:
Nz Class:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace NozCoreWebApp5.Models
{
public partial class Nz
{
public Nz()
{
Mkh = new HashSet<Mkh>();
}
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:#########}")]
public decimal TxtId { get; set; }
public int Sn { get; set; }
public string FullName { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime? BirthDate { get; set; }
public int SexCode { get; set; }
public string InsertUser { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime InsertDate { get; set; }
public ICollection<Mkh> Mkh { get; set; }
}
}
Mkh Class: (child for Nz)
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace NozCoreWebApp5.Models
{
public partial class Mkh
{
public int Sn { get; set; }
public decimal TxtId { get; set; }
public int MkhCode { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime MkhDate { get; set; }
public int Days { get; set; }
public string Notes { get; set; }
public string InsertUser { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime InsertDate { get; set; }
public MkhType MkhCodeNavigation { get; set; }
public Nz Txt { get; set; }
}
}
MkhType Calss: (Parent of Mkh)
using System;
using System.Collections.Generic;
namespace NozCoreWebApp5.Models
{
public partial class MkhType
{
public MkhType()
{
Mkh = new HashSet<Mkh>();
}
public int MkhCode { get; set; }
public string MkhName { get; set; }
public string InsertUser { get; set; }
public DateTime InsertDate { get; set; }
public ICollection<Mkh> Mkh { get; set; }
}
}
and I have this context:
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
namespace NozCoreWebApp5.Models
{
public partial class NozContext : DbContext
{
public NozContext()
{
}
public NozContext(DbContextOptions<NozContext> options)
: base(options)
{
}
public virtual DbSet<Mkh> Mkh { get; set; }
public virtual DbSet<MkhType> MkhType { get; set; }
public virtual DbSet<Nz> Nz { get; set; }
public virtual DbSet<Sex> Sex { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
optionsBuilder.UseSqlServer("Server=My-PC\\SQLEXPRESS;Database=Noz;Trusted_Connection=True;");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Mkh>(entity =>
{
entity.HasKey(e => e.Sn);
entity.ToTable("Mkh");
entity.Property(e => e.Sn).HasColumnName("sn");
entity.Property(e => e.Days).HasColumnName("days");
entity.Property(e => e.InsertDate)
.HasColumnName("insert_date")
.HasColumnType("datetime")
.HasDefaultValueSql("(getdate())");
entity.Property(e => e.InsertUser)
.IsRequired()
.HasColumnName("insert_user")
.HasMaxLength(20)
.HasDefaultValueSql("(N'any')");
entity.Property(e => e.MkhCode).HasColumnName("Mkh_code");
entity.Property(e => e.MkhDate)
.HasColumnName("Mkh_date")
.HasColumnType("date");
entity.Property(e => e.Notes).HasColumnName("notes");
entity.Property(e => e.TxtId)
.HasColumnName("txt_id")
.HasColumnType("numeric(9, 0)");
entity.HasOne(d => d.MkhCodeNavigation)
.WithMany(p => p.Mkh)
.HasForeignKey(d => d.MkhCode)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_Mkh_Mkh_type");
entity.HasOne(d => d.Txt)
.WithMany(p => p.Mkh)
.HasForeignKey(d => d.TxtId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_Mkh_Nz");
});
modelBuilder.Entity<MkhType>(entity =>
{
entity.HasKey(e => e.MkhCode);
entity.ToTable("Mkh_type");
entity.Property(e => e.MkhCode).HasColumnName("Mkh_code");
entity.Property(e => e.InsertDate)
.HasColumnName("insert_date")
.HasColumnType("datetime")
.HasDefaultValueSql("(getdate())");
entity.Property(e => e.InsertUser)
.IsRequired()
.HasColumnName("insert_user")
.HasMaxLength(20)
.HasDefaultValueSql("(N'any')");
entity.Property(e => e.MkhName)
.IsRequired()
.HasColumnName("Mkh_name")
.HasMaxLength(50);
});
modelBuilder.Entity<Nz>(entity =>
{
entity.HasKey(e => e.TxtId);
entity.ToTable("Nz");
entity.Property(e => e.TxtId)
.HasColumnName("txt_id")
.HasColumnType("numeric(9, 0)");
entity.Property(e => e.BirthDate)
.HasColumnName("birth_date")
.HasColumnType("date");
entity.Property(e => e.InsertDate)
.HasColumnName("insert_date")
.HasColumnType("datetime")
.HasDefaultValueSql("(getdate())");
entity.Property(e => e.InsertUser)
.IsRequired()
.HasColumnName("insert_user")
.HasMaxLength(20)
.HasDefaultValueSql("(N'any')");
entity.Property(e => e.SexCode).HasColumnName("sex_code");
entity.Property(e => e.Sn)
.HasColumnName("sn")
.ValueGeneratedOnAdd();
entity.HasOne(d => d.SexCodeNavigation)
.WithMany(p => p.Nz)
.HasForeignKey(d => d.SexCode)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_Nz_sex");
});
modelBuilder.Entity<Sex>(entity =>
{
entity.HasKey(e => e.SexCode);
entity.ToTable("sex");
entity.Property(e => e.SexCode).HasColumnName("sex_code");
entity.Property(e => e.SexName)
.IsRequired()
.HasColumnName("sex_name")
.HasMaxLength(10);
});
modelBuilder.Entity<Nz>()
.HasAlternateKey(x => x.Sn).HasName("IX_Sn");
}
}
}
and the NzController actions:
public class NzController : Controller
{
private readonly NozContext _context;
public NzController(NozContext context)
{
_context = context;
}
// ........ Some Code here .............
public async Task<IActionResult> Edit(decimal? id)
{
if (id == null)
{
return NotFound();
}
//var Nz = await _context.Nz.FindAsync(id);
var Nz = await _context.Nz
.Include(n => n.Mkh)
.ThenInclude(m => m.MkhCodeNavigation)
.FirstOrDefaultAsync(n => n.TxtId == id.Value);
if (Nz == null)
{
return NotFound();
}
ViewData["SexCode"] = new SelectList(_context.Sex, "SexCode", "SexName", Nz.SexCode);
return View(Nz);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(decimal id, [Bind("TxtId,Sn,FullName,BirthDate,SexCode,InsertUser,InsertDate")] Nz Nz)
{
if (id != Nz.TxtId)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
var Edited = new Nz()
{
TxtId = Nz.TxtId,
BirthDate = Nz.BirthDate,
InsertDate = Nz.InsertDate,
InsertUser = Nz.InsertUser,
FullName = Nz.FullName,
SexCode = Nz.SexCode,
Mkh = Nz.Mkh.Where(m => m.TxtId == Nz.TxtId).ToList(),
};
_context.Update(Edited);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!NzExists(Nz.TxtId))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
ViewData["SexCode"] = new SelectList(_context.Sex, "SexCode", "SexName", Nz.SexCode);
return View(Nz);
}
the partial view 'EditNz' in Mkh controller:
@model IEnumerable<Mkh><h4>MkhTbl</h4><hr /><table class="table"><thead><tr><th>
Date</th><th>
Days</th><th>
Notes</th><th>
Mkh Type</th><th></th></tr></thead><tbody>
@foreach (var item in Model)
{<tr><td>
@Html.EditorFor(modelItem => item.MkhDate)</td><td>
@Html.EditorFor(modelItem => item.Days)</td><td>
@Html.EditorFor(modelItem => item.Notes)</td><td>
@Html.EditorFor(modelItem => item.MkhCodeNavigation.MkhName)</td></tr>
}</tbody></table>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
and the calling of the previous partial in Edit view in NzController:
@{ await Html.RenderPartialAsync("~/Views/Mkh/EditNz.cshtml", Model.Mkh); }
Now when I press save button (submit) no data is saved for the partial one like days! Why? and How to solve please?
and How to return Select with returned values from MkhType? (using MkhCodeNavigation I think)