How to gracefully handle an AJAX PartialView update with Javascript disabled

I finally managed to update PartialView with Ajax. The purpose of partial viewing is a sidebar widget that displays the contents of the registration and allows you to remove items from the registration.

An abbreviated version of PartialView is as follows:

<table id="item-entries">
    @foreach (var item in Model.Items)
    {
        <tr>
            <td>@item.Name</td>
            <td>@item.Price</td>
            <td>
                @using (Ajax.BeginForm("RemoveItemEntry", "Registration", new AjaxOptions { UpdateTargetId = "item-entries" }))
                {
                    <button type="submit" name="ItemId" value="@item.ItemId">×</button>
                }
            </td>
        </tr>
    }
</table>

And here is a shortened action example:

[HttpPost]
public ActionResult RemoveItemEntry(ItemViewModel data)
    {
      // Perform logic to remove the item from the registration 
      // then return the PartialView with updated model

      return PartialView("~/Views/Partials/ItemEntries.cshtml", model);
    }
}

This works fine now, however I do not want to offer a broken experience for those who have JavaScript disabled. If you publish a form with JavaScript disabled, the action still performs correctly, but you are redirected to a URL that displays a PartialView and nothing more. I would like users who have JavaScript disabled to be redirected back to the original page from which the form was submitted.

Is this achievable?

+4
2

, :

- Action :

[HttpPost]
public ActionResult RemoveItemEntry(ItemViewModel data)
{
    // Perform logic to remove the item from the registration 
    // then return the PartialView with updated model

    if (Request.IsAjaxRequest())
    {
          return PartialView("~/Views/Partials/ItemEntries.cshtml", model);
    }
    else
    {
          // return the initial View not the parial fie
    }
}

, Ajax , Action, . jQuery, AJAX , AJAX , PartialView.

+2

:

:

<script>
    document.cookie = "JSEnabled=1; path=/";
</script>

JS, POST, cookie Request, -

enter image description here

JavaScript, cookie , -

enter image description here

, cookie, null , () .

+2

Source: https://habr.com/ru/post/1528090/


All Articles