Ajax用mvc开始表单db记录

时间:2017-12-27 15:44:29

标签: asp.net-mvc html-form asp.net-mvc-ajax html-post

我创建了一个表单。我想做一个保存后的方法。

他记录但两次记录相同的数据。我该如何解决这个问题?

我必须解决双重注册问题。我按下按钮一次。当我逐步完成调试时,它会在同一行上运行两次。当我自上而下控制数据库时,我看到你是双重记录的。

HTML:

<div class="portlet-body form">
       @using (Ajax.BeginForm("TalepTurKaydet", "MasterEducationController",
         new AjaxOptions { HttpMethod = "post", OnSuccess = "TalepTurKaydet" },
                                                            new { @class = "" }))
        {
            @Html.ValidationSummary(true)
            <div class="form-body">
                <div class="row">               
                    <div class="col-md-12" style="margin-left: 20px">

                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="control-label col-md-3">Açıklama: </label>
                            <div class="col-md-9">
                                <textarea id="Aciklama" name="Aciklama" class="col-md-12" style="resize: none;" rows="5" placeholder="Açıklama"></textarea>
                            </div>
                        </div>
                    </div>
                    <div class="clearfix"></div><br /><br />
                </div>
                <div class=" form-actions right">
                    <button type="button" class="btn green btnPrevious"><i class="fa fa-angle-double-left"></i>Geri</button>
                    <button id="talepOlustur" type="submit" class="btn blue"><i class="fa fa-check"></i> Talep Oluştur</button>
                </div>
            </div>
        }
    </div>

控制器:

public ActionResult MezuniyetBilgiKaydet(MezuniyetBilgi model)
        {
            List<MezuniyetBilgi> list = null;
            model.KullaniciId = GetUye().kullaniciID;
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(ApiAdress.API_URL);

                var responseTask = client.PostAsJsonAsync("apiMasterProgramEducation/MezuniyetBilgiKaydet", model);
                responseTask.Wait();

                var result = responseTask.Result;
                if (result.IsSuccessStatusCode)
                {
                    var readTask = result.Content.ReadAsAsync<List<MezuniyetBilgi>>();
                    readTask.Wait();

                    list = readTask.Result;
                    model = list.FirstOrDefault();

                    return Json(new
                    {
                        Success = true,
                        data = model,
                        Message = SuccessMessage.MEZUNIYET_BILGISI_INSERT_MESSAGE,
                        JsonRequestBehavior.AllowGet
                    });

                }

            }
        }

API:

   public IHttpActionResult MezuniyetBilgiKaydet(MezuniyetBilgi model)
        {
            List<MezuniyetBilgi> detay = new List<MezuniyetBilgi>();
            try
            {
                using (var ctx = new ktdbEntities())
                {
                    if (model != null)
                    {
                        var query = ctx.mezuniyetBilgisiEkle(model.KullaniciId, model.MezuniyetTarih, model.MezunOlduguOkul,
                            model.MezunOlduguFakulte, model.MezunOlduguBolum, (float)(model.MezuniyetNotu));

                        model.Output = true;
                        detay.Add(model);
                        return Ok(detay);
                    }

                }
            }
            catch (Exception e)
            {
                model.Output = false;
                model.Message = e.Message;
                detay.Add(model);

            }
            return Ok(detay);
        }

0 个答案:

没有答案