Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Value cannot be null when no records saved for partial!

$
0
0

Hi All

I have a main edit page consists of a lot of partial views called in it, When I'm trying to update data on the main edit page itself or any of its partial data, The data is updated successfully if there is at least one record of the called partial or else does not save and the action gives me this error: (when there is no records of the partial called)

ArgumentNullException: Value cannot be null.
Parameter name: source

The main model:

    public partial class Guests
    {
        public decimal Id { get; set; }
        public string FName { get; set; }
        public string SName { get; set; }
        public string ThName { get; set; }
        public string LName { get; set; }
        [Required]
        public string FullName { get; set; }
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
        [Required]
        public DateTime? BirthDate { get; set; }
        [Required]
        public int? SexId { get; set; }
        [Required]
        public int? HalaId { get; set; }
        public string ImageUrl { get; set; }
        public string UserId { get; set; }
        public DateTime InsertDate { get; set; }
        public string UpdateUser { get; set; }
        public DateTime? UpdateDate { get; set; }
        public Hala Hala { get; set; }
        public Sex Sex { get; set; }
        public ApplicationUser User { get; set; }

        public IList<Visit> Visit { get; set; }
    }

The child Visit model:

public partial class Visit
    {
        public int Id { get; set; }
        [Required]
        public decimal GuestId { get; set; }

        public DateTime VisitDate { get; set; }
        public int? VisitTypeId { get; set; }
        public bool SpecialStatus { get; set; }
        public string Notes { get; set; }
        public string UserId { get; set; }
        public DateTime InsertDate { get; set; }
        public string UpdateUser { get; set; }
        public DateTime? UpdateDate { get; set; }

        public Guests Guest { get; set; }
        public VisitType VisitType { get; set; }
        public ApplicationUser User { get; set; }
    }

The post Edit method of Guests controller:

 [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(decimal id, [Bind("Id,FName,SName,ThName,LName,FullName,BirthDate,SexId,HalaId,KafalaId,ImageUrl,UserId,InsertDate,UpdateUser,UpdateDate,Visit")] Guests guest)
        {
            if (id != guest.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var Edited = new Guests()
                    {
                        Id = guest.Id,
                        BirthDate = guest.BirthDate,
                        InsertDate = guest.InsertDate,
                        UserId = guest.UserId,
                        FullName = guest.FullName,
                        SexId = guest.SexId,
                        HalaId = guest.HalaId,
                        FName = guest.FName,
                        SName = guest.SName,
                        ThName = guest.ThName,
                        LName = guest.LName,
                        ImageUrl = guest.ImageUrl,
                        UpdateUser = guest.UpdateUser,
                        UpdateDate = guest.UpdateDate,

                        Visit = guest.Visit.Where(m => m.GuestId == guest.Id).ToList()/
                    };
                    _context.Update(Edited);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!GuestsExists(guest.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }

            return View(guest);
        }

The Guests edit view:

@model Guests<meta charset="utf-8" dir="rtl"><meta name="viewport" content="width=device-width, initial-scale=1"><form asp-action="Edit"><div asp-validation-summary="ModelOnly" class="text-danger"></div><input type="hidden" asp-for="Id" /><input type="hidden" asp-for="InsertDate" /><input type="hidden" asp-for="UserId" /><input type="hidden" id="hdn_ha" asp-for="HalaId" /><div class="row"><div class="col-md-4"><h3>Guest Edit</h3></div><div class="col-md-4"></div></div><hr style="margin:0" /><div class="row"><div class="col-md-3"><label asp-for="FullName" class="control-label">Full Name</label><input asp-for="FullName" class="form-control" readonly /><span asp-validation-for="FullName" class="text-danger"></span></div><div class="col-md-3"><label asp-for="BirthDate" class="control-label">Birth Date</label><input asp-for="BirthDate" class="form-control" readonly /><span asp-validation-for="BirthDate" class="text-danger"></span></div><div class="col-md-3"><label asp-for="SexId" class="control-label">Sex</label><select asp-for="SexId" class="form-control" asp-items="ViewBag.SexId"></select><span asp-validation-for="SexId" class="text-danger"></span></div><div class="col-md-3"><label asp-for="HalaId" class="control-label">Hala</label><select asp-for="HalaId" class="form-control" id="ha" asp-items="ViewBag.HalaId" disabled></select><span asp-validation-for="HalaId" class="text-danger"></span></div></div><br /><div><ul class="nav nav-tabs"><li class="in active"><a data-toggle="tab" href="#home">Visits</a></li><li><a data-toggle="tab" href="#menu2">Another</a></li></ul><div class="tab-content">
            @*<div id="home" class="tab-pane fade in active">
            @{ await Html.RenderPartialAsync("~/Views/Visit/ParVisit.cshtml", Model); }</div></div><div class="form-group"><input type="submit" value="Save" class="btn btn-default" /></div></form>


@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}<script src="https://code.jquery.com/jquery-1.11.0.min.js"></script><script>$(document).ready(function () {
        var select_ha = $('#ha option:selected').val();$('#hdn_ha').val(select_ha);
    });</script>

The ParVisit view:

@model Guests<a asp-action="Create" asp-controller="Visit"><img src="~/images/icons8-add-new-480.png" style="background-color:blue" height="30" width="30" /></a><table class="table"><thead><tr><th>
                Visit Date</th><th>
                Visit Type</th><th>
                Special Status</th><th>
                Notes</th></tr></thead><tbody>
        @for (var i = 0; i < Model.Visit.Count; ++i)
        {<tr>
            @Html.HiddenFor(m => m.Visit[i].Id)
            @Html.HiddenFor(m => m.Visit[i].GuestId)
            @Html.HiddenFor(m => m.Visit[i].InsertDate)
            @Html.HiddenFor(m => m.Visit[i].UpdateDate)
            @Html.HiddenFor(m => m.Visit[i].UserId)
            @Html.HiddenFor(m => m.Visit[i].UpdateUser)
            @Html.HiddenFor(m => m.Visit[i].VisitTypeId)
            @Html.HiddenFor(m => m.Visit[i].Guest)<td width="15%">
                @Html.EditorFor(m => m.Visit[i].VisitDate)</td><td width="15%">
                @Html.DropDownListFor(m => m.Visit[i].VisitTypeId, (SelectList)ViewBag.VisitTypeId)</td><td width="15%">
                @Html.EditorFor(m => m.Visit[i].SpecialStatus)</td><td width="50%">
                @Html.EditorFor(m => m.Visit[i].Notes)</td></tr>
        }</tbody></table>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

Now if there is one record at least in Visit table the action is saving changes correctly unless it gives me this error:

ArgumentNullException: Value cannot be null.
Parameter name: source

................................................

ArgumentNullException: Value cannot be null. Parameter name: source

  • System.Linq.Enumerable.Where<TSource>(IEnumerable<TSource> source, Func<TSource, bool> predicate)

  • NozCoreWebApp7.Controllers.GuestsController.Edit(decimal id,

    Guests guest) in GuestsController.cs

<div class="source">

  1. }
  2. if (ModelState.IsValid)
  3. {
  4. try
  5. {
  1. var Edited = new Guests()
  1. {
  2. Id = guest.Id,
  3. BirthDate = guest.BirthDate,
  4. InsertDate = guest.InsertDate,
  5. UserId = guest.UserId,
  6. FullName = guest.FullName,

Why? and How to solve please?


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>