Check which submit button has been clicked in ASP.Net MVC

Last Reply 14 days ago By dharmendr

Posted 15 days ago

Hi

How to pass variable value from View to Controller. Suppose user clicked on Updated button i want to pass a value to controller that UPdate button has been clicked say "U"

Suppose user clicked on Add button i want to pass a value to controller that Add button has been clicked say "A"

I have below code. Is it possible to Add/Edit without Ajax.

How i can check whether user has clicked on Add or Update

    <body>
        <div class="table-title">
            <div class="row">
                <div class="col-sm-6 text-right">
                    <button type="button" class="btn btn-primary" data-target="#myModal" data-toggle="modal" float-right onclick="clearTextBox();"> <i class="fa fa-plus"></i> Add New Location</button><br /><br />
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-xs-12">
                <div class="box">
                    <div class="box-body">

                        <table class="table table-striped table-hover" id="tblProduct">
                            <thead>
                                <tr>
                                    <th>
                                        @Html.DisplayNameFor(model => model.Id)
                                    </th>
                                    <th>
                                        @Html.DisplayNameFor(model => model.IsActive)
                                    </th>
                                    <th>Action</th>
                                </tr>
                            </thead>

                            @foreach (var item in Model)
                            {
                                <tr>
                                    <td>
                                        @Html.DisplayFor(modelItem => item.Id)
                                    </td>
                                    <td>
                                        @Html.DisplayFor(modelItem => item.IsActive)
                                    </td>
                                    <td>
                                        <a class='btn btn-primary btn-sm' id='btnEdit' data-target="#myModal" data-toggle="modal"><i class='fa fa-pencil'></i> Edit </a>

                                    </td>
                                </tr>
                            }
                        </table>
                    </div>
                </div>
            </div>
        </div>

        @using (Html.BeginForm("CreateEdit", "Location", FormMethod.Post))
        {
            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                        </div>
                        <div class="modal-body">
                            <div class="form-group row">
                                <label for="Id" class="col-sm-2 col-form-label">Id</label>
                                <div class="col-sm-10">
                                    <input type="Text" class="form-control txt-style" id="txtId" Name="Id" placeholder="Id">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label for="IsActive" class="col-sm-2 col-form-label">IsActive</label>
                                <div class="form-check col-sm-10">
                                    <input class="form-check-input" type="checkbox" value="" id="IsActive" name="IsActive">
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="submit" class="btn btn-primary">Save changes</button>
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                        </div>
                    </div>
                </div>
            </div>
        }
    </body>

 

        [HttpPost]
        public ActionResult CreateEdit(Location objLocation)
        {
            if (ModelState.IsValid)
            {
                dbLocation.Add(objLocation);
                //return Json(new { success = true, message = "Successfully Saved" });
            }

            return RedirectToAction("Index");
        }

 

Thanks

You are viewing reply posted by: dharmendr 14 days ago.
Posted 14 days ago Modified on 14 days ago

Hi ramco1917,

Check this example. Now please take its reference and correct your code.

Database

I have made use of the following table Customers with the schema as follows.

I have already inserted few records in the table.

You can download the database table SQL by clicking the download link below.

Download SQL file

Model

public class CustomerModel
{
    public int CustomerID { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
    public bool IsActive { get; set; }
    public List<Customer> Customers { get; set; }
}

Controller

public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        using (CustomerEntities entities = new CustomerEntities())
        {
            CustomerModel model = new CustomerModel();
            model.Customers = entities.Customers.ToList();
            return View(model);
        }
    }

    [HttpPost]
    public ActionResult Index(CustomerModel model, string hfAU)
    {
        using (CustomerEntities entities = new CustomerEntities())
        {
            if (hfAU == "U")
            {
                Customer customerUpdate = entities.Customers.Where(x => x.CustomerId == model.CustomerID).FirstOrDefault();
                customerUpdate.Name = model.Name;
                customerUpdate.Country = model.Country;
                entities.SaveChanges();
            }
            if (hfAU == "A")
            {
                entities.Customers.Add(new Customer
                {
                    Name = model.Name,
                    Country = model.Country
                });
                entities.SaveChanges();
            }
            model.Customers = entities.Customers.ToList();
            return View(model);
        }
    }

    public ActionResult DeleteCustomer(int id)
    {
        using (CustomerEntities entities = new CustomerEntities())
        {
            Customer customer = entities.Customers.Where(x => x.CustomerId == id).FirstOrDefault();
            entities.Customers.Remove(customer);
            entities.SaveChanges();

            CustomerModel model = new CustomerModel();
            model.Name = customer.Name;
            model.Country = customer.Country;
            model.Customers = entities.Customers.ToList();

            return View("Index", model);
        }
    }
}

View

@model CheckBox_Popup_MVC.Models.CustomerModel
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
    <link href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" />
    <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#tblCustomers").dataTable();
            $('[id*=btnEdit]').on('click', function () {
                $('#hfAU').val('U');
                var tds = $(this).closest('tr').find('td');
                $('#hfId').val($(tds).eq(0).html());
                $('#txtName').val($(tds).eq(1).html());
                $('#txtCountry').val($(tds).eq(2).html());
                if ($(tds).eq(3).find('input[type=checkbox]').is(':checked')) {
                    $('#IsActive').attr('checked', 'checked');
                    $('#hfIsActive').val('true');
                } else {
                    $('#IsActive').removeAttr('checked');
                    $('#hfIsActive').val('false');
                }
                $('#myModal').modal('show');
            });

            $('#IsActive').click(function () {
                if ($(this).is(':checked')) {
                    $('#hfIsActive').val('true');
                } else {
                    $('#hfIsActive').val('false');
                }
            });

            $('#btnAdd').click(function () {
                $('#hfAU').val('A');
            });
        });
    </script>
</head>
<body class="container">
    <div class="table-title">
        <div class="row">
            <div class="col-sm-6 text-right">
                <button id="btnAdd" type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
                    <i class="fa fa-plus"></i> Add New
                </button>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-body">
                    <table class="table" id="tblCustomers">
                        <thead>
                            <tr>
                                <th>Id</th>
                                <th>Name</th>
                                <th>Country</th>
                                <th>IsActive</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model.Customers)
                            {
                                var active = item.Country == "India" ? true : false;
                                <tr>
                                    <td>@Html.DisplayFor(modelItem => item.CustomerId)</td>
                                    <td>@Html.DisplayFor(modelItem => item.Name)</td>
                                    <td>@Html.DisplayFor(modelItem => item.Country)</td>
                                    <td>
                                        @Html.DisplayFor(modelItem => active)
                                    </td>
                                    <td>
                                        <a class='btn btn-primary btn-sm' id='btnEdit'><i class='fa fa-pencil'></i> Edit</a>
                                        <a href="@Url.Action("DeleteCustomer","Home")/@item.CustomerId" onclick="return confirm('Do you want to delete this record?')"
                                           class='btn btn-danger btn-sm' id='btnDelete' style='margin-left:5px'><i class='fa fa-trash'></i>Delete</a>
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {
        <input type="hidden" id="hfAU" name="hfAU" />
        <div id="myModal" class="modal" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Customer Details Form</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <div class="form-group">
                            <input type="hidden" id="hfId" name="CustomerID" />
                            <label>Name:</label>
                            <input type="text" id="txtName" name="Name" value="" class="form-control" />
                        </div>
                        <div class="form-group">
                            <label>Country:</label>
                            <input type="text" id="txtCountry" name="Country" value="" class="form-control" />
                        </div>
                        <div class="form-group">
                            <label for="IsActive" class="col-sm-2 col-form-label">IsActive</label>
                            <div class="form-check col-sm-10">
                                <input class="form-check-input" type="checkbox" value="" id="IsActive">
                                <input type="hidden" id="hfIsActive" name="IsActive" />
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary">Save changes</button>
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    </div>
                </div>
            </div>
        </div>
    }
</body>
</html>

Screenshot