简体   繁体   中英

how can I show the user that the data is gone when he trying to edit a field?

so when the user click the edit link to edit one of the client (field) and another user have erase already that client how can show to the user that the client (field) is gone?

so I'am using TempData is another way to do it? i think jquery but i don't know how to use it properly

public ActionResult Edit (int id)
        {
            client cliente = db.Clients.Find(id);
            if (cliente != null)
            {
                return View(cliente);
            }
            TempData["message"] = string.Format("this client have be erase for other user");
            return RedirectToAction("Index");

        }

edit:

and view is this

<table class="widgets">
    <tr>
    <th></th>
        <th>
             @Html.ActionLink("Nombre", "Index", new { ordenacion = ViewBag.NameSortParm, filtro = ViewBag.filtro })
        </th>

    </tr>

@foreach (var item in Model) {
    <tr id="widget-id-@item.id">
         <td>
             @Html.ActionLink("Editar", "Edit", new { id=item.id })  |

                      @Ajax.ActionLink("Eliminar", "Eliminar", "Cliente",
                new {item.id },
                new AjaxOptions {
                    HttpMethod = "POST",
                    Confirm = string.Format("Esta Seguro que quiere eliminar '{0}'?", item.descripcion),
                    OnSuccess = "deleteConfirmation"
                })
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.descripcion)
        </td>

    </tr>
}
</table>

i guees the script will be this? so i have to make my edit link like i did for the delete (eliminar) link using @Ajax.ActionLink right?

<script type="text/javascript">

        var validateForEdit = function (id) {
            var validateCallbackFunction = function (result) {
                if (result) {
                    window.location = '/Client/Editar/' + id;
                }
                else {
                    window.Alert('this client have be erase for other user');
                }
            };

            $.post('/Client/ValidateForEdit/', { id: id }, validateCallbackFunction, 'json');
        }

</script>

Hi you can use following code to validate data before user can edit that

   var validateForEdit = function (id) {
        var validateCallbackFunction = function (result) {
            if (result) {
                window.location = '/Client/Edit/' + id;
            }
            else {
                Alert('this client have be erase for other user');
            }
        };

        $.post('/Client/ValidateForEdit/', { id: id }, validateCallbackFunction, 'json');
    }

And your Action :

    [HttpPost]
    public JsonResult ValidateForEdit(int id)
    {
        var cliente = db.Clients.Find(id);
        return cliente != null ? Json(true) : Json(false);
    }

Edit : And you have to replace your following code

@Html.ActionLink("Editar", "Edit", new { id=item.id })

with this code :

<input class="button" type="button" value="Edit" onclick="validateForEdit(item.id)" />

Hope this help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM