簡體   English   中英

根據選定的復選框更新數據庫

[英]Updating database based on selected check boxes

我有以下代碼在我的視圖中生成動態復選框:

@foreach (var sub in Model)
{
    <div class="row">
        <div class="col-md-3">
            <input type="checkbox" 
                   value="@sub.Id" 
                   name="Profile.InterestIds"
                   checked="@sub.IsChecked"/>
        </div>
        <div class="col-md-6">
            @sub.Title
        </div>
    </div>
}

在此應用程序中,我們使用軟刪除機制,因此,如果用戶未選中某項,則只需使用_memberInterestService.Delete(id, item.Id, true);IsDeleted設置為true _memberInterestService.Delete(id, item.Id, true); 這是執行此過程的代碼:

public override void Update(UserProfileDto item)
{
    // Getting all Member Interests (where IsDeleted == true)
    var memberIntersestIds = _memberInterestService.GetAllPlusDeleted();
    // List of ids in the database as Guid
    var dbIds = memberIntersestIds.Select(p => p.InterestSubCategoryId);
    var union = dbIds.ToList().Union(item.InterestIds);

    // item.InterestIds is IEnumerable<Guid> (selected check boxes)
    if (item.InterestIds != null)
    {
        var guids = union as Guid[] ?? union.ToArray();
        foreach (var id in guids)
            if (_memberInterestService.IsSubInterestExist(id))
                _memberInterestService.Delete(id, item.Id, true);

        foreach (var id in guids)
        {
            if (!_memberInterestService.IsSubInterestExist(id))
                _memberInterestService.Add(new MemberInterestDto
                {
                    UserProfileId = item.Id,
                    InterestSubCategoryId = id
                });
            else
                _memberInterestService.Delete(id, item.Id, false);
        }
    }
    else
        foreach (var memberInterest in memberIntersestIds)
            _memberInterestService.Delete(memberInterest.InterestSubCategoryId, item.Id, true);
}  

此代碼可以正常工作,直到用戶在提交表單后未取消選中復選框之一之前,此代碼不會將未選中的項目從數據庫中刪除。 任何想法?

更新 :通過將foreach循環更改為for循環,我使代碼正常工作,因為每個復選框的ID必須是唯一的:

@for (int i = 0; i < Model.Count(); i++)
{
    <div class="row">
        <div class="col-md-3">
            <input type="checkbox" id="@i" 
                   value="@Model.ElementAt(i).Id" 
                   name="Profile.InterestIds" 
                   class = "interest-checkbox" 
                   checked="@Model.ElementAt(i).IsChecked"/>
        </div>
        <div class="col-md-6">
            @Model.ElementAt(i).Title
        </div>
    </div>
}

然后,我將方法從使用Union更改為Except :( 也使用了此建議

public override void Update(UserProfileDto item)
{
    var memberIntersestIds = _memberInterestService.GetMyInterestsExceptDeleted(item.Id);
    if (item.InterestIds != null)
    {
        var dbIds = memberIntersestIds.Select(p => p.InterestSubCategoryId);
        var guids = dbIds.ToList().Except(item.InterestIds).ToArray();
        foreach (var id in guids)
        {
            if (_memberInterestService.IsSubInterestExist(id, item.Id))
            {
                _memberInterestService.Delete(id, item.Id, true);
            }
            else
            {
                _memberInterestService.Add(new MemberInterestDto
                {
                    UserProfileId = item.Id,
                    InterestSubCategoryId = id
                });
            }
        }
    }
    else
    {
        foreach (var memberInterest in memberIntersestIds)
        {
            _memberInterestService.Delete(memberInterest.InterestSubCategoryId, item.Id, true);
        }
    }
}

現在,我可以取消選中項目,但是不會將其重新選中,因為它們存在於數據庫中並且其IsDeleted設置為1

乍看起來,您所有復選框的name="Profile.InterestIds"都為name="Profile.InterestIds" 那可能是個問題。

我相當確定這是因為您有兩次foreach循環

  foreach (var id in guids) if (_memberInterestService.IsSubInterestExist(id)) _memberInterestService.Delete(id, item.Id, true); foreach (var id in guids) { if (!_memberInterestService.IsSubInterestExist(id)) _memberInterestService.Add(new MemberInterestDto { UserProfileId = item.Id, InterestSubCategoryId = id }); else _memberInterestService.Delete(id, item.Id, false); } 

第一個刪除所有數據,第二個將數據重新添加回。

您的代碼當前刪除存在的所有內容,然后添加不存在的內容或刪除仍然存在的內容。

這段代碼對我來說沒有意義。 我這樣看

遍歷id並刪除所有存在的內容,現在_memberInterestService不存在任何_memberInterestService

然后再次遍歷所有id,添加不存在的所有內容,如果仍然存在,請刪除它,使其不再存在

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM