How can i post a nested model which contains ienumerable model

Muhammed Vaseem 0 Reputation points
2023-07-16T13:47:26.2533333+00:00

<div class="col-md-10" id="LvformContainer">
		<form method="post" enctype="multipart/form-data" data-ajax="true">
			<div class="row">
				<div class="col-md-8">
					<div class="row">
						<div class="col-md-8">
							<div class="form-group d-flex align-items-center">
								<label class="form-label me-2">Document</label>
								<div class="form-check me-2">
									<input class="form-check-input" type="checkbox">
								</div>
								<input class="form-control" value=" @ViewBag.LvDoc">
								<button class="btn me-2" id="loadDocModalButton" data-toggle="modal" data-target="#DocNoDtModal"><i class="fa-solid fa-table"></i></button>
								<button class="btn btn-primary">
									<i class="fa-solid fa-pen-to-square"></i>
								</button>
							</div>
						</div>
						<div class="col-md-4">
							<div class="form-group d-flex align-items-center">
								<label class="form-label me-2">Document Date</label>
							    <input class="form-control" asp-for="LvLedger.TrDate" type="date" >
							</div>
						</div>
					</div>
					<div class="form-group d-flex align-items-center mb-2">
						<label class="form-label me-2">Employee</label>
						<div class="form-group d-flex align-items-center me-2">
							<input class="form-control" type="text" name="EmpDets.EmpId" asp-for="EmpDets.EmpId">
							<button class="btn" id="loadModalButton" data-toggle="modal" data-target="#EmpDtModal"><i class="fa-solid fa-table"></i></button>
						</div>
 
						<input class="form-control" type="text" name="EmpDets.EmpName" asp-for="EmpDets.EmpName">
						<span></span>
					</div>
					<div class="form-group d-flex align-items-center ">
						<label class="form-label me-2">Description</label>
						<input class="form-control" asp-for="LvLedger.Remark" type="text">
						<span></span>
					</div>
 
				</div>
				<div class="col-md-4">
				<div class="form-group ">
						<label class="form-label justify-content-center me-2">Address</label>
						<textarea class="form-control" asp-for="LvLedger.Address" type="text"></textarea>
					</div>
				</div>
			</div>
		
		<div class="form-group d-flex align-items-center mt-3 bg-primary">
			<div class="form-check m-1">
				<input class="form-check-input" type="checkbox">
			</div>
			<label class="form-label ">Leave Excemption</label>
			<label class="form-label mx-auto text-light">Leave Assigning</label>
		</div>
		<section class="card-container ">
			<div class="table-responsive" style="height:200px;">
				<table class="table table-striped" style="max-width:150%; table-layout:auto;">
					<thead>
						<tr>
							<th>Sl.No</th>
							<th>Type</th>
							<th>Name</th>
							<th>(-)</th>
							<th>From</th>
							<th>To</th>
							<th>(-)</th>
							<th>Days</th>
							<th>Wk/Off</th>
							<th>Holidays</th>
							<th>Excess</th>
							<th>Applied</th>
							<th>Wage</th>
							<th>Remark</th>
							<th></th>
						</tr>
					</thead>
					<tbody id="LvInputTb">
						@if (Model.lvLedgerDets is not null)
						{
							@foreach (var item in Model.lvLedgerDets)
							{
								<tr>
 
									<td>@item.SlNo</td>
									<td>
										<div class="form-group d-flex align-items-center">@item.LvTpNo
											@*<button class="btn" id="loadLvModalButton" data-toggle="modal" data-target="#LvTpModal"><i class="fa-solid fa-table"></i></button>*@
										</div>
									</td>
									<td>@item.LvTypeName </td>
									<td>@item.BeginDed </td>
									<td>
										@(item.StartDt != null ? item.StartDt.Value.ToString("dd-MM-yyyy") : string.Empty)
 
									</td>
									<td>@(item.EndDt != null ? item.EndDt.Value.ToString("dd-MM-yyyy") : string.Empty)</td>
									<td>@item.EndDed </td>
									<td>@item.PrdDays</td>
									<td>@item.WkDays</td>
									<td>@item.HoliDays</td>
									<td></td>
									<td>@item.NetDays</td>
									@foreach (var lvtp in Model.LvDef)
									{
										if (lvtp.LvDefNo == item.LvTpNo)
										{
											<td>@lvtp.IsWageView</td>
										}
 
									}
									<td>@item.LdgrRemark</td>
									<td>
										<a href="javascript:void(0)" class="btn-link me-2 edit-btn" onclick="editRow(@item.SlNo)">Edit</a>
                                        <a href="javascript:void(0)" class="btn-link" onclick="deleteRow(@item.SlNo)">Delete</a>
									</td>	
								</tr>
							}
						}
					</tbody>
				</table>
				@if (Model.lvLedgerDets is not null)
					{
						@for (var i = 0; i < Model.lvLedgerDets.Count; i++)
						{
							<input type="hidden" asp-for="@Model.lvLedgerDets[@i].SlNo" />
							<input type="hidden" asp-for="@Model.lvLedgerDets[@i].LvTpNo"  />
							<input type="hidden" asp-for="@Model.lvLedgerDets[@i].LvTypeName"  />
							<input type="hidden" asp-for="@Model.lvLedgerDets[@i].BeginDed" />
							<input type="hidden" asp-for="@Model.lvLedgerDets[@i].StartDt"  />
							<input type="hidden" asp-for="@Model.lvLedgerDets[@i].EndDt" />
							<input type="hidden" asp-for="@Model.lvLedgerDets[@i].EndDed"  />
							<input type="hidden" asp-for="@Model.lvLedgerDets[@i].PrdDays" />
							<input type="hidden" asp-for="@Model.lvLedgerDets[@i].WkDays"  />
							<input type="hidden" asp-for="@Model.lvLedgerDets[@i].HoliDays" />
							<input type="hidden" asp-for="@Model.lvLedgerDets[@i].NetDays"  />
							<input type="hidden" asp-for="@Model.lvLedgerDets[@i].LdgrRemark" />
						}
					}
					
			</div>
		</section>
		<div class="form-group d-flex align-items-center mt-3">
			<div class="form-check m-1">
				<input class="form-check-input" type="checkbox">
			</div>
			<label class="form-label">Avoid AL Salary Booking</label>
			<div class="form-group ms-auto">
				@*<button class="btn" id="addRowButton">Add</button>*@
				<button class="btn" id="AddLvDtButton" data-toggle="modal" data-target="#AddLvDtModal">Add</button>
				@*<button class="btn">Remove</button>
				<button class="btn">Insert</button>*@
			    <button class="btn" type="submit" id="verifyButton">Verify</button>
			</div>
		</div>
		</form>
</div> 


public class LvDefViewModel
	{
		//public long DocNo { get; set; }
		public List<LvLedgerDet> lvLedgerDets { get; set; }
		public LvLedger? LvLedger { get; set; }
		public IEnumerable<LvDef>? LvDef { get; set; }
		//public LvLedgerDet? LvLedgerDetInput { get; set; }
		public EmpDet? EmpDets { get; set; }
	}


[HttpPost]
		public IActionResult VerifyLeave([FromBody]LvDefViewModel model)
		{
			if (ModelState.IsValid)
			{
				try
				{
					
					return PartialView("~/Views/Shared/Leave/_VerifyView.cshtml", model);
				}
				catch (JsonException)
				{
					ModelState.AddModelError("", "Invalid JSON payload");
				}
				catch (Exception ex)
				{
					ModelState.AddModelError("", $"An error occurred: {ex.Message}");
				}
			}
 
			// If ModelState is invalid or an error occurred, return an error response
			var errors = ModelState.Values.SelectMany(v => v.Errors.Select(e => e.ErrorMessage));
			return BadRequest(errors);
		}


The problem i encounder is i am not able to post the data to C# ihad uploaded the js i used for posting

Developer technologies | ASP.NET | ASP.NET Core
Microsoft 365 and Office | Development | Office JavaScript API
Developer technologies | ASP.NET | Other
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,926 Reputation points Volunteer Moderator
    2023-07-16T15:53:09.5733333+00:00

    You can’t. The data binder can not create an instance of an abstract type when its copies values to the model. In your model either define IEnumerable’s to a concrete type or initialize them to a concrete type.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.