当没有保存部分记录时,值不能为空

时间:2019-04-24 05:54:24

标签: asp.net-core-2.1 sql-server-2017

我有一个主编辑页面,其中包含许多局部视图,当我尝试更新主编辑页面本身上的数据或其任何局部数据时,如果存在,则数据更新成功至少一个被调用部分的记录不保存,否则该操作给我这个错误:(当没有被调用部分的记录时)

ArgumentNullException:值不能为null。 参数名称:来源

主要型号:

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

儿童访问模型:

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

Guests控制器的post Edit方法:

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

客人编辑视图:

@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>

ParVisit视图:

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

现在,如果至少在Visit表中有一条记录,则该操作将正确保存更改,除非它给我这个错误:

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

}
if (ModelState.IsValid)
{
try
{ 

var Edited = new Guests() 

{
Id = guest.Id,
BirthDate = guest.BirthDate,
InsertDate = guest.InsertDate,
UserId = guest.UserId,
FullName = guest.FullName, 

为什么?如何解决好吗?

0 个答案:

没有答案