如何在插入数据库时​​避免重复值

时间:2016-07-14 13:37:42

标签: c# jquery ajax asp.net-mvc

我知道这个问题已被提出,但我也需要学习。我想在你注册时检查身份证号码,但我不确定我该怎么做。现在我正在添加验证,但一切都在保存。

这是我到目前为止所做的,但我不知道如何通过Id号字段来检查它是否存在。您的指导将不胜感激。感谢

我的代码:

     public async Task<bool> AddFleet(FleetModel fleet)
     {
        bool IsDone = false;
        try
        {
            using (SidDbContext db = new SidDbContext())
            {
                FleetVehicle fv = new FleetVehicle()
                {
                    BranchId = fleet.BranchId,
                    Description = fleet.Description,
                    Registration = fleet.Registration

                };
                db.FleetVehicle.Add(fv);
                db.SaveChanges();

                Fleet _fleet = new Fleet
                {
                    FitmentTeamId = Convert.ToInt32(fleet.FitmentTeamId),
                    Capacity = fleet.Capacity,
                    BranchId = fleet.BranchId,
                    Active = true                     
                };
               var results = db.Fleet.Add(_fleet);
                db.SaveChanges();

                db.FleetVehicleLink.Add(new FleetVehicleLink { FleetId = _fleet.Id ,FleetVehicleId = fv .Id});
                db.SaveChanges();
                //add team

                foreach (var u in fleet.TeamMember1)
                {
                    db.FitmentUsersLink.Add(new FitmentUsersLink { FitmentTeamId = _fleet.Id, UsersId = u });                       
                }

                db.SaveChanges();

                IsDone = true;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return IsDone = true;
    }


function RegistrationFunction() {
try {

    $.support.cors = true;
    var registration = {
        Idnumber: $('#txtIdnumber').val(),
    }

Js代码:

    $.ajax({

        url: BaseUrl + "Registrations/RegistrationMethod",
        type: "Post",
        dataType: "Json",
        registration: registration,

        if(registration)
        {
            var form = $(this)
            swal({

                title: "Oops! Duplicate Student ID Number......",
                text: "ID Number "+ $('#txtIdnumber').val()+ " already being used",
                Idnumber: $('#txtIdnumber').val(),
                type: "Warning",
                showCancelButton: true                
            });
        }
    });

0 个答案:

没有答案