Home > Back-end >  Is there a way to reload a single Partialview without loading the whole site? in asp.net Razorpages
Is there a way to reload a single Partialview without loading the whole site? in asp.net Razorpages

Time:05-20

Hello guys i wanted to ask if there is a way to reload a single partial after a is clicked instead of loading the whole site new.

My site looks like this. Page when i load it for the first time

My code

This is how i load my partialview when i go to my main site.

        <div>
            @{
                await Html.RenderPartialAsync("_ArticlePositonsPartial",Model.ArticlePositions, ViewData);
            }
        </div>

Model for the partial

model IList<Rechnungen.Models.Position>

Partial html

<form enctype="multipart/form-data">
<div >
    <table  id="tablepost">
        <thead >
            <tr>
                <th style="width:72.5px" >Anzahl</th>
                <th style="width:72.5px" >Einheit</th>
                <th style="width:320px" >Nr   Bezeichnung</th>
                <th style="width:135px" >Stk Preis.</th>
                <th style="width:135px" >Ges Preis.</th>
                <th style="width:75px" ></th>
            </tr>
        </thead>
        <tbody id="tablebodypost">
            @foreach (var item in Model)
            {
                  <tr id="">
                    <td style="width:72.5px" >
                        <span >@item.ArticleAmount</span>
                    </td>
                    <td style="width:72.5px" >
                        <span >@item.ArticleId</span>
                    </td>
                    <td style="width:320px; font-size:12px;" >
                        <span >test</span>
                    </td>
                    <td style="width:135px" >
                        <span >test </span>
                    </td>
                    <td style="width:135px;" >
                        <span > test</span>
                    </td>
                    <td style="width:75px" >
                        <a  data-id="1" data-toggleToolTip="tooltip" data-placement="top" title="Entfernen">
                            <i ></i>
                        </a>
                    </td>
                </tr>
            }
              
            
        </tbody>
    </table>
</div>

So after i Click on the button "Hinzufügen" i want to add something to a list and reload the tablePartial

  <form mmethod="post" asp-page-handler="AddPosition">
                        <button  type="submit" data-added="0"><i ></i>Hinzufügen</button>
  </form>

Method in codebehind:

  public PartialViewResult OnPostAddPosition()
    {
        ArticlePositions.Add(new Position { ArticleAmount = 1, ArticleId = 2 });
        return new PartialViewResult
        {
            ViewName = "_ArticlePositonsPartial",
            ViewData = new ViewDataDictionary<IList<Position>>(ViewData, ArticlePositions),
           
        };
    }

After the PostMethod is called my site looks like this: Page after calling the PostMethod

I have looked it up in the interned already and found nothing that helped me. Is there a way to only reload the TablePartial?

CodePudding user response:

Try to remove form and use button to call js function,and use ajax to call handler,here is a demo:

cshtml:

<div id="MyPartialView">
    @{
        await Html.RenderPartialAsync("_ArticlePositonsPartial", Model.ArticlePositions, ViewData);
    }
</div>
@Html.AntiForgeryToken()
<button data-added="0" onclick="GetPartialView()"><i ></i>Hinzufügen</button>

js:

function GetPartialView() {
            $.ajax({
                type: "POST",
                url: '?handler=AddPosition',
                headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
                success: function (data) {
                    $("#MyPartialView").html(data);
                },
                error: function (result) {
                    alert("fail");
                }
            })
        }

CodePudding user response:

I have changed the code:

[BindProperty]
public List<Position> ArticlePositions { get; set; } = new List<Position>();

 public PartialViewResult OnPostAddPosition([FromBody] List<Position> articlePositions)
    {
        Random myObject = new Random();
        Random myObject1 = new Random();

        articlePositions.Add(new Position
        {
            ArticleAmount = myObject1.Next(10, 100),
            ArticleId = myObject.Next(10, 100)
        });
        var data = new NewInvoiceModel(_AddressRepository, _InvoiceNumberRangeRepository, _AddressTelephoneRepository, _ArticleRepository, _ITextsRepository, _HeaderDataRepository);
        data.ArticlePositions = articlePositions;
        var myViewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()) { { "_ArticlePositonsPartial", articlePositions } };
        myViewData.Model = data;
        return new PartialViewResult
        {
            ViewName = "_ArticlePositonsPartial",
            ViewData = myViewData,

        };
    }

HTML:`

<div id="MyPartialView">
 @{
   await Html.RenderPartialAsync("_ArticlePositonsPartial", Model, ViewData);
 }
</div>`

Javascript:

function GetPartialView() {

        var model = @Json.Serialize(Model.ArticlePositions);
        $.ajax({
            type: "POST",
            url: '?handler=AddPosition',
            data: JSON.stringify(model),
            dataType: "html",
            contentType: "application/json",
            headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
            success: function(data) {
                $("#MyPartialView").html(data);
            },
            error: function(result) {
                alert("fail");
            }
        });
    }
  • Related