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

Last Reply one month ago By dharmendr

Posted one month 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

This question does not have replies that have been liked.