2

MVC update data in an index view

 3 years ago
source link: https://www.codesd.com/item/mvc-update-data-in-an-index-view.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

MVC update data in an index view

advertisements

I have an index view listing a bunch of information, example:

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.FullName)
        </td>

        <td>
            @Html.DisplayFor(modelItem => item.OptIn)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Donation)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Attended)
        </td>
        <td>
            @Html.ActionLink("Add One", "Check", new { id = item.ID }, new { @class = "btn btn-primary btn-default" })
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
            @Html.ActionLink("Details", "Details", new { id = item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.ID })
        </td>
    </tr>
}

I want to set up an ADD ONE button here. Notice the line above that says:

<td>
    @Html.ActionLink("Add One", "Check", new { id = item.ID }, new { @class = "btn btn-primary btn-default" })
</td>

This is easy If I go to another controller/view however I want to stay in the index view. I built a CHECK controller action result as follows:

public ActionResult Check(int? id)
//[Bind(Include = "ID, Attended")]
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Guest guest = db.Guests.Find(id);
    guest.Attended = (guest.Attended + 1);
    if (ModelState.IsValid)
    {
        db.Entry(guest).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return RedirectToAction("Edit/" + id);
}

This is not working. The page doesn't crash, but it is not doing anything. I am guessing maybe I need to use JQUERY for this? I havent learned JQUERY yet so I'm trying to avoid that. Has anyone accomplished something like this before? I think I'm on the right track but I am stuck at this point....


Make sure JQuery library is added then create script like : $(window).load(function () { var data = $('#yourInputId').val();

               $.ajax({
                url: "http://" + location.host + "/CTRL/ACTION",
                type: "POST",
                data: { 'deleteId': data },
            }).done(function (data) {
                your logic here if server;
            }).fail(function (err) {
                alert(err.statusText);
            });
});




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK