Link to home
Start Free TrialLog in
Avatar of Crystal Rouse
Crystal RouseFlag for United States of America

asked on

Check for duplicate values when Adding a New Record in the Controller Method

I have a form with multiple Text fields.  I need to check when the user completes the form if all of the fields match an entry already in the database.  If so, return a message and send them back to another View - perhaps the one matching their entry.  

Here is my form:  I have several more fields but just included a few as an example

    @using (Html.BeginForm("Add", "MyController", FormMethod.Post, new
    {
        name = "NewForm"
    }))
    {

        <div class="panel panel-info" style="position: relative;">
            <div class="panel-heading">Add Information</div>
            <div class="panel-body">
                <div class="col-xs-12">

                    <div class="row" style="margin-top: 0; margin-bottom: 5px;">
                        <div class="col-xs-6">
                            <div style="color:red;padding-bottom:0px; display:inline">*</div>
                            @Html.Label("Select Field 1")

                        </div>
                    </div>
                    <div class="row" style="margin-top: 0; margin-bottom: 10px;">

                        <div class="col-xs-6">

                            @Html.DropDownListFor(x => x.field1, sl.getField1List(), new
                       {
                           @type = "drop",
                           @id = "field1",
                       })
                            <div class="error">
                                @Html.ValidationMessageFor(x => x.field1)
                            </div>
                        </div>
                    </div>

                    <div class="row" style="margin-top: 0; margin-bottom: 5px;">
                        <div class="col-xs-6">
                            <div style="color:red;padding-bottom:0px; display:inline">*</div>
                            @Html.Label("Select Field 2")
                        </div>
                    </div>

                    <div class="row" style="margin-top: 0;  margin-bottom: 10px;">

                        <div class="col-xs-6">

                            @Html.DropDownListFor(x => x.Field2, sl.getField2List(), new
                       {
                           @type = "drop",
                           @id = "field2",
                       })
                            <div class="error">
                                @Html.ValidationMessageFor(x => x.field2)
                            </div>
                        </div>
                   </div>

                    <div class="row" style="margin-top: 0;">
                        <div class="col-xs-4">
                            <div style="color:red;padding-bottom:0px; display:inline">*</div>
                            @Html.Label("Enter Field3")
                            @Html.TextBoxFor(x => x.field3, new {
                           @class = "form-control",
                           @maxlength = "255",
                           @placeholder = "Enter Field 3",
                           @required = "required"
                       })
                            <div class="error">
                                @Html.ValidationMessageFor(x => x.field3)
                            </div>

                        </div>

                        <div class="col-xs-4">
                            <div style="color:red;padding-bottom:0px; display:inline">*</div>
                            @Html.Label("Enter Field 4")
                            @Html.TextBoxFor(x => x.field4, new {
                                @class = "form-control",
                                @maxlength = "255",
                                @placeholder = "Enter Field 4",
                                @required = "required"
                            })
                            <div class="error">
                                @Html.ValidationMessageFor(x => x.field4)
                            </div>
                        </div>

                </div>

                    <div class="row">
                        <div class="col-xs-12">
                            @Html.ActionLink("Cancel", "Index", "myController", null, new {
                                @class = "btn btn-danger",
                                @style = "float: left; margin-left: 20px"
                            })

                            <button type="submit" class="btn btn-success" style="float: right;" onclick="add.formSubmit(this.form);">
                                Add
                            </button>
                        </div>
                    </div>

                </div>
        </div>
    }

Open in new window


My controller method looks like this:

   [HttpPost]
        public ActionResult Add(newRequest request)
        {
            var result = new { Success = "", Message = "" };
            if (!user.Admin())
                return RedirectToAction("Index", "myController");

            if (!ModelState.IsValid)
                return View(request);
            try
            {
                
                tbl_Data newData = new tbl_Data()
                {
                    field1 = request.field1
                    field2 = request.field2
                    field3 = request.field3
                    field4 = request.field4

      };

                DB.tbl_Data.Add(newData);
                DB.SaveChanges();
            }

            catch (Exception ex)
            {
                Utility util = new Utility();
                string error = util.innerExcep(ex);
                result = new { Success = "False", Message = error };
                return RedirectToAction("Index", "myController");
            }

            return RedirectToAction("Index", "MyController");
        }

Open in new window


What I need to do is check if all the fields 1-x text equals the same fields in the database and return a message to the user if they exist, then return the user to that record.  It is only a duplicate if the contents of all fields are the same as a current record.
ASKER CERTIFIED SOLUTION
Avatar of Crystal Rouse
Crystal Rouse
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial