首页 > 解决方案 > Asp.net 在创建新类别时添加重复条目

问题描述

问题是,当我试图添加一个新的“类别”时,它会重复......

这是我的控制器

public async Task<IActionResult> AddOrEdit(int id, [Bind("Id,Name,GroupsId,keywords,HasBattery,IsElectronic,IsToxic,HasMemory,IsFurniture,HasBulb,Obsolete,DateModification")] Category category)
    {
        if (ModelState.IsValid)
        {
            //Do INSERT operation
            if (id == 0)
            {
                category.DateModification = DateTime.Now;
                _context.Add(category);
                await _context.SaveChangesAsync();
                TempData["message"] = "Insertion";
                return RedirectToAction(nameof(Index));
            }

这是我的ajax帖子

jQueryAjaxPost = form => {
    try {
        $.ajax({
            type: 'POST',
            url: form.action,
            data: new FormData(form),
            contentType: false,
            processData: false,
            success: function (res) {
                console.log("success")                        
                },
            error: function (err) {
                console.log(err);
            }
        })

    } catch (e) {
        console.log(e);
    }

不知道我还能显示什么,我有一个类似的删除代码,但它没有显示错误消息,告诉我它正在尝试删除已删除的内容

<form asp-action="AddOrEdit" asp-route-id="@Model.Id" onsubmit="return jQueryAjaxPost(this);" autocomplete="off">
    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
    <input type="hidden" asp-for="Id" />
    <input type="hidden" asp-for="DateModification" />
    <input type="hidden" asp-for="Obsolete"  />
    <div class="form-group row">
        <label asp-for="Name" class="control-label col-2"></label>
        <input asp-for="Name" class="form-control col-10" required/>
        <span asp-validation-for="Name" class="text-danger"></span>
    </div>
    
    <div class="form-group row">
        <label asp-for="GroupsId" class="control-label col-2">Groupe</label>
        <select asp-for="GroupsId" class="form-control col-10" asp-items="ViewBag.GroupsId"></select>
        <span asp-validation-for="GroupsId" class="text-danger"></span>
    </div>
    <div class="form-group row">
        <label asp-for="keywords" class="control-label col-2">Mot clés</label>
        <textarea asp-for="keywords" class="form-control col-10"></textarea>
        <span asp-validation-for="keywords" class="text-danger"></span>
    </div>

    <div class="form-group row">
        <div class="col-md-2">
            <span class="svg-icon svg-icon-2x" data-toggle="tooltip" title="Batterie" data-theme="dark" data-placement="top" id="iconBattery" onclick="changeState('HasBattery', this.id)">
                <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24px" height="24px" viewBox="0 0 24 24" version="1.1">
                    <g stroke="none" stroke- width="1" fill="none" fill- rule="evenodd">
                        <rect x="0" y="0" width="24" height="24" />
                        <path d="M11,9 L11,15 L17,15 L17,9 L11,9 Z M4,7 L17,7 C18.1045695,7 19,7.8954305 19,9 L19,15 C19,16.1045695 18.1045695,17 17,17 L4,17 C2.8954305,17 2,16.1045695 2,15 L2,9 C2,7.8954305 2.8954305,7 4,7 Z" fill="#000000" fill-rule="nonzero" />
                        <path d="M20,10 L21,10 C21.5522847,10 22,10.4477153 22,11 L22,13 C22,13.5522847 21.5522847,14 21,14 L20,14 L20,10 Z" fill="#000000" opacity="0.3" />
                    </g>
                </svg>
                <span id="badgeBattery" class="badge badge-pill badge-primary">?</span>
            </span>
            
            <input type="hidden" asp-for="HasBattery" class="form-control" />
            <span asp-validation-for="HasBattery" class="text-danger"></span>
        </div>
        <div class="col-md-2">
            <span class="svg-icon svg-icon-2x" data-toggle="tooltip" title="Electronic" data-theme="dark" data-placement="top" id="iconElectronic" onclick="changeState('IsElectronic', this.id)">
                <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24px" height="24px" viewBox="0 0 24 24" version="1.1">
                    <g stroke="none" stroke- width="1" fill="none" fill- rule="evenodd">
                        <rect x="0" y="0" width="24" height="24" />
                        <path d="M15,4 C15.5522847,4 16,4.44771525 16,5 L16,9 L14,9 L14,5 C14,4.44771525 14.4477153,4 15,4 Z M9,4 C9.55228475,4 10,4.44771525 10,5 L10,9 L8,9 L8,5 C8,4.44771525 8.44771525,4 9,4 Z" fill="#000000" opacity="0.3" />\
                        <path d="M13,16.9291111 L13,22 L11,22 L11,16.9291111 C7.60770586,16.4438815 5,13.5264719 5,10 L5,9 L19,9 L19,10 C19,13.5264719 16.3922941,16.4438815 13,16.9291111 Z" fill="#000000" />\
                    </g>
                </svg>
                <span id="badgeElectronic" class="badge badge-pill badge-primary">?</span>
            </span>
            <input type="hidden" asp-for="IsElectronic" class="form-control" />
        </div>
        <div class="col-md-2">
            <span class="svg-icon svg-icon-2x" data-toggle="tooltip" title="Toxic" data-theme="dark" data-placement="top" id="iconToxic" onclick="changeState('IsToxic', this.id)">
                <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24px" height="24px" viewBox="0 0 24 24" version="1.1">
                    <g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
                        <rect x="0" y="0" width="24" height="24" />
                        <path d="M11.1669899,4.49941818 L2.82535718,19.5143571 C2.557144,19.9971408 2.7310878,20.6059441 3.21387153,20.8741573 C3.36242953,20.9566895 3.52957021,21 3.69951446,21 L21.2169432,21 C21.7692279,21 22.2169432,20.5522847 22.2169432,20 C22.2169432,19.8159952 22.1661743,19.6355579 22.070225,19.47855 L12.894429,4.4636111 C12.6064401,3.99235656 11.9909517,3.84379039 11.5196972,4.13177928 C11.3723594,4.22181902 11.2508468,4.34847583 11.1669899,4.49941818 Z" fill="#000000" opacity="0.3" />\
                        <rect fill="#000000" x="11" y="9" width="2" height="7" rx="1" />
                        <rect fill="#000000" x="11" y="17" width="2" height="2" rx="1" />
                    </g>
                </svg>
                <span id="badgeToxic" class="badge badge-pill badge-primary">?</span>
            </span>
            <input type=hidden asp-for="IsToxic" class="form-control" />
            <span asp-validation-for="IsToxic" class="text-danger"></span>
        </div>
        <div class="col-md-2">
            <span class="svg-icon svg-icon-2x" data-toggle="tooltip" title="Mémoire" data-theme="dark" data-placement="top" id="iconMemory"  onclick="changeState('HasMemory', this.id)">
                <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24px" height="24px" viewBox="0 0 24 24" version="1.1">
                    <g stroke="none" stroke- width="1" fill="none" fill- rule="evenodd">
                        <rect x="0" y="0" width="24" height="24" />
                        <path d="M11.5,17.4497475 L6.55025253,12.5 C6.15972824,12.1094757 6.15972824,11.4763107 6.55025253,11.0857864 L13.6213203,4.01471863 C14.4023689,3.23367004 15.6686989,3.23367004 16.4497475,4.01471863 L19.9852814,7.55025253 C20.76633,8.33130112 20.76633,9.59763107 19.9852814,10.3786797 L12.9142136,17.4497475 C12.5236893,17.8402718 11.8905243,17.8402718 11.5,17.4497475 Z" fill="#000000" />\
                        <path d="M6.90380592,19.9246212 L4.0753788,17.0961941 C3.88011665,16.9009319 3.88011665,16.5843494 4.0753788,16.3890873 L6.55025253,13.9142136 L10.0857864,17.4497475 L7.6109127,19.9246212 C7.41565056,20.1198833 7.09906807,20.1198833 6.90380592,19.9246212 Z" fill="#000000" opacity="0.3" />\
                    </g>
                </svg>
                <span id="badgeMemory" class="badge badge-pill badge-primary">?</span>
            </span>
            <input type="hidden" asp-for="HasMemory" class="form-control" />
            <span asp-validation-for="HasMemory" class="text-danger"></span>
        </div>
        <div class="col-md-2">
            <span class="svg-icon  svg-icon-2x" data-toggle="tooltip" title="Meuble" data-theme="dark" data-placement="top" id="iconFurniture"  onclick="changeState('IsFurniture', this.id)">
                <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24px" height="24px" viewBox="0 0 24 24" version="1.1">
                    <g stroke="none" stroke- width="1" fill="none" fill- rule="evenodd">
                        <rect x="0" y="0" width="24" height="24" />
                        <path d="M12,2 C13.8385982,2 15.5193947,3.03878936 16.3416408,4.68328157 L19,10 C20.365323,12.730646 19.25851,16.0510849 16.527864,17.4164079 C15.7602901,17.8001948 14.9139019,18 14.0557281,18 L9.94427191,18 C6.8913169,18 4.41640786,15.525091 4.41640786,12.472136 C4.41640786,11.6139622 4.61621302,10.767574 5,10 L7.65835921,4.68328157 C8.48060532,3.03878936 10.1614018,2 12,2 Z M7.55,13.6 C9.00633458,14.6922509 10.4936654,15.25 12,15.25 C13.5063346,15.25 14.9936654,14.6922509 16.45,13.6 L15.55,12.4 C14.3396679,13.3077491 13.1603321,13.75 12,13.75 C10.8396679,13.75 9.66033208,13.3077491 8.45,12.4 L7.55,13.6 Z" fill="#000000" />\
                        <path d="M6.15999985,21.0604779 L8.15999985,17.5963763 C8.43614222,17.1180837 9.04773263,16.9542085 9.52602525,17.2303509 C10.0043179,17.5064933 10.168193,18.1180837 9.89205065,18.5963763 L7.89205065,22.0604779 C7.61590828,22.5387706 7.00431787,22.7026457 6.52602525,22.4265033 C6.04773263,22.150361 5.88385747,21.5387706 6.15999985,21.0604779 Z M17.8320512,21.0301278 C18.1081936,21.5084204 17.9443184,22.1200108 17.4660258,22.3961532 C16.9877332,22.6722956 16.3761428,22.5084204 16.1000004,22.0301278 L14.1000004,18.5660262 C13.823858,18.0877335 13.9877332,17.4761431 14.4660258,17.2000008 C14.9443184,16.9238584 15.5559088,17.0877335 15.8320512,17.5660262 L17.8320512,21.0301278 Z" fill="#000000" opacity="0.3" />\
                    </g>
                </svg>
                <span id="badgeFurniture" class="badge badge-pill badge-primary">?</span>
            </span>
            <input type="hidden" asp-for="IsFurniture" class="form-control" />
            <span asp-validation-for="IsFurniture" class="text-danger"></span>
        </div>
        <div class="col-md-2">
            <span class="svg-icon svg-icon-2x" data-toggle="tooltip" title="Ampoule" data-theme="dark" data-placement="top" id="iconBulb" onclick="changeState('HasBulb', this.id)">
                <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24px" height="24px" viewBox="0 0 24 24" version="1.1">
                    <g stroke="none" stroke- width="1" fill="none" fill- rule="evenodd">
                        <rect x="0" y="0" width="24" height="24" />
                        <circle fill="#000000" opacity="0.3" cx="12" cy="9" r="8" />
                        <path d="M14.5297296,11 L9.46184488,11 L11.9758349,17.4645458 L14.5297296,11 Z M10.5679953,19.3624463 L6.53815512,9 L17.4702704,9 L13.3744964,19.3674279 L11.9759405,18.814912 L10.5679953,19.3624463 Z" fill="#000000" fill-rule="nonzero" opacity="0.3" />\
                        <path d="M10,22 L14,22 L14,22 C14,23.1045695 13.1045695,24 12,24 L12,24 C10.8954305,24 10,23.1045695 10,22 Z" fill="#000000" opacity="0.3" />
                        <path d="M9,20 C8.44771525,20 8,19.5522847 8,19 C8,18.4477153 8.44771525,18 9,18 C8.44771525,18 8,17.5522847 8,17 C8,16.4477153 8.44771525,16 9,16 L15,16 C15.5522847,16 16,16.4477153 16,17 C16,17.5522847 15.5522847,18 15,18 C15.5522847,18 16,18.4477153 16,19 C16,19.5522847 15.5522847,20 15,20 C15.5522847,20 16,20.4477153 16,21 C16,21.5522847 15.5522847,22 15,22 L9,22 C8.44771525,22 8,21.5522847 8,21 C8,20.4477153 8.44771525,20 9,20 Z" fill="#000000" />\
                    </g>
                </svg>
                <span id="badgeBulb" class="badge badge-pill badge-primary">?</span>
            </span>
            
            <input type="hidden" asp-for="HasBulb" class="form-control" />
            <span asp-validation-for="HasBulb" class="text-danger"></span>
        </div>
    </div>
    <div class="form-group">
        <input id="createBtn" type="submit" value="Soumettre" class="btn btn-primary btn-block" />
    </div>
</form>

编辑:添加了我的 htlm 的一部分,因为它可能会有所帮助

标签: javascriptasp.netasp.net-mvc

解决方案


推荐阅读