如何使用asp.net mvc检查数据库中是否存在重复数据

时间:2021-03-07 03:17:57

I want to show a message saying that the JourneyID already exists in the database.

我想显示一条消息,说JourneyID已存在于数据库中。

The model is :

该模型是:

using System.Web.Mvc;
namespace Project_Final.Models
{
using System;

public partial class sp_FMS_Group6_Module3_ViewBonusMilesRequesttt_Result
{
    public int RequestID { get; set; }
    public Nullable<long> CustomerID { get; set; }
    [Remote("Check", "Home", ErrorMessage = "Bonus Miles Request has already been sent!")]
    public Nullable<int> JourneyID { get; set; }
    public Nullable<System.DateTime> RequestDate { get; set; }
    public string Status { get; set; }
}
}

The following are the actions in my controller :

以下是我的控制器中的操作:

[HttpPost]
public ActionResult Index(string Create)
{        
        FMS_Group6_Module3_BonusMilesRequestt objProd = new FMS_Group6_Module3_BonusMilesRequestt();
        objProd.CustomerID = int.Parse(Request.Form["CustomerID"].ToString());
        objProd.JourneyID = int.Parse(Request.Form["JourneyID"].ToString());
        objProd.RequestDate = DateTime.Parse(Request.Form["RequestDate"].ToString());
        objProd.Status = "Pending";

        objDB.FMS_Group6_Module3_BonusMilesRequestt.Add(objProd);

        int i = objDB.SaveChanges();
        if (i > 0)
            ViewBag.Message = "Product details saved successfully";
        return Content("<script language='javascript' type='text/javascript'>alert('Your Bonus Miles Request has been successfully sent!');window.location='/Home/GetID'</script>");
        //return Redirect("GetID");
}

public ActionResult Check(string Crreate)
{
        FMS_Group6_Module3_BonusMilesRequestt objProd = new FMS_Group6_Module3_BonusMilesRequestt();
        bool ifJourneyIDExist = false;
        try

        {

            ifJourneyIDExist = Crreate.Equals(objProd.JourneyID) ? true : false;

            return Json(!ifJourneyIDExist, JsonRequestBehavior.AllowGet);

        }

        catch (Exception ex)

        {

            return Json(false, JsonRequestBehavior.AllowGet);

        }
}

Any kind of help would be greatly appreciated. Thanks.

任何形式的帮助将不胜感激。谢谢。

2 个解决方案

#1


1  

Before you insert, you can check in the DB, if the JourneyID is exists.

在插入之前,如果JourneyID存在,您可以签入数据库。

if (objDB.FMS_Group6_Module3_BonusMilesRequestt.Any(x => x.JourneyID == objProd.JourneyID))
{
    // Exists
}

#2


0  

LINQ query to make sure all values (ex enumerable) are not duplicated

LINQ查询以确保所有值(ex enumerable)不重复

var noDuplicationVariable = enumerableValues.GroupBy(a => a.Key).All(b => b.Count() == 1);

use it as an assertion condition before your function

在函数之前将它用作断言条件

#1


1  

Before you insert, you can check in the DB, if the JourneyID is exists.

在插入之前,如果JourneyID存在,您可以签入数据库。

if (objDB.FMS_Group6_Module3_BonusMilesRequestt.Any(x => x.JourneyID == objProd.JourneyID))
{
    // Exists
}

#2


0  

LINQ query to make sure all values (ex enumerable) are not duplicated

LINQ查询以确保所有值(ex enumerable)不重复

var noDuplicationVariable = enumerableValues.GroupBy(a => a.Key).All(b => b.Count() == 1);

use it as an assertion condition before your function

在函数之前将它用作断言条件