0

I have index view, that have list of products in partialview, with Edit and Delete buttons. Edit button call 2nd level Edit partialview. There is button to launch update, but it works anyway as Http post ActionResult even as void and with [NonAction] attribute. It is probably due to that Edit partial view loads as Get Action, and so any button in it, that sends updates to db, work as Post Action. And there is no way now to return this update method except either index or edit.cshtml!

    ProductController:Controller ...
    //Get Edit 
    public PartialViewResult Edit(int id)
    {
        var product = productRepo.GetProduct(id);
        return PartialView("_Edit", product);
    }
    [NonAction] //Public or private all return "_Edit" partialview
    void Edit(Product product)
    {    //ActionResult Edit(Product product)
        try
        {
            productRepo.UpdateProduct(product);
            //return new EmptyResult();
            //return PartialView();
        }
        catch
        {
          //return PartialView("_Edit");
        }
    }

_Edit partial view:

    <script>
    $(document).ready(function () {
        $("#updateProduct").click(function () {
        $("#updateProduct").parent().parent().parent()
        .find("#allProducts").load('/Shared/_ShowAll');        
         // .load('@Url.Content("/Shared/_ShowAll")')
        });
    });

//By clicking update button it should load new version of allProducts DIV with _ShowAll partial view after product update (or delete)

 @using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Product</legend>
    @Html.HiddenFor(model => model.Id)...
........
   <p>
        <input id="updateProduct" type="submit" value="Update" />
    </p>

What I can to do to run productRepo.UpdateProduct(product); as non-action method without loading partialview as full-page mainview? And then call Jquery .load('/Shared/_ShowAll') in upper-level view that should be possible. The only hardly semi-solution is to redirectToAction("Index"), but it reload mainview, and all partialview got rolled up. An then I need to open ListofProduct partialview additionally. What other options is for my approach of CRUD SPA with PartialViews in ASP.NET MVC?

art
  • 59
  • 1
  • 7
  • `$("#updateProduct").parent().parent().parent().find("#allProducts")` <= side note that this should be just `$('#allProducts')`. IDs are expected to be unique within a given page, so there should not be a need to perform this contextual type of logic. – Taplar May 27 '20 at 23:06
  • I cannot check if it is workable as I got blank or partial view page when trying to launch my code, which excerpts are here. That means the loading Edit post actionresult do not allow to get the moment of Jquery loading, as initial DOM structure is destroyed. Indeed $("#hideAllProucts").parent().parent().find("#allProducts").empty() that I implemented yesterday works - but there is just one first-level partial view, so Jquery function parent() is called just twice in a chain. So my ID is unique, and I use such Jquery logic to float up from second level to zero-level mainview. – art May 27 '20 at 23:59
  • Just $('#allProducts') doesnot work as it called from child view, that is not accessible from there. Using parent()'s worked for DIV clearing from child view. – art May 28 '20 at 00:02
  • https://stackoverflow.com/questions/26833065/asp-net-mvc-how-to-call-void-controller-method-without-leaving-the-view -- here is similar question, that is the part of my overall question, as it relates just calling void method without leaving current view. It seems that topic also have no answer, except RedirectToAction("ActionName") that return full-page view. So subsequent script $("#updateProduct")...load('/Shared/_ShowAll'), - that is bound to submit button, that initially induce post ActionResult Edit view, - cannot prevent action beforehand?! So the only way out to prevent returning view? – art May 28 '20 at 13:34
  • I tried one more option - instead launch Edit/Update action by button, to lauch it with ActionLink with inbuild OnComplete JS function that would load new updated list (partialview) of products - @Ajax.ActionLink("Update","Edit",Model,new AjaxOptions { //UpdateTargetId="holder", OnComplete="refresh", HttpMethod = "Post" })

    But after clicking ActionLink - no result. Nothing change in view and updating product???
    – art May 28 '20 at 20:30

0 Answers0