How can I refresh just a Partial View in its View?

前端 未结 3 696
北恋
北恋 2021-02-03 11:30

What Am I doing wrong guys? This is the idea...

Index view

@Html.Partial(\"PartialView\", Model)
相关标签:
3条回答
  • 2021-02-03 11:58

    Partial view is oriented to reuse some parts of the code in differents views but when you submit a form, the entire view is reloaded.

    If you dont want to reload yo have to use AJAX for submit the form.

    0 讨论(0)
  • 2021-02-03 12:07

    Well, I read the solution (Auto Refresh Partial View). I am posting it, hoping clarify the question:

    index.html

    <div class="col-lg-12 col-md-12 col-xs-12" id="divPartial">
        @Html.Partial("PartialView", Model)
    </div>
    
    <script type="text/javascript">
      $("#buttonForm").click(function(e){
        $('#form').submit();
        $('#divPartial').load('/PartialController/PartialView'); 
      });
    </script>
    

    PartialController

    public ActionResult PartialView()
    {
        // DO YOUR STUFF. 
        return PartialView("PartialView", model);
    }
    
    [HttpPost, ValidateInput(false)]
    public EmptyResult POSTPartialView(string param1)
    {
        // DO YOUR STUFF AFTER SUBMIT.
        return new EmptyResult();
    } 
    
    0 讨论(0)
  • 2021-02-03 12:11

    Partial view is for reuse some parts of the code in different. When a page is submit then view is reload if you want to avoid page refreshing, you can use ajax.

    Code for View

    $.ajax({
        type: "POST",
        url: '@Url.Action("ControllerName", "ActionName")',
        contentType: "application/json; charset=utf-8",
        data: { data: "yourdata" },
        dataType: "json",
        success: function(recData) { alert('Success'); },
        error: function() { alert('A error'); }
    });
    

    Code for Controller

    public JsonResult ActionName(string yourdata)
    {
       return Json(p); \\ where p is you want to return
    }
    
    0 讨论(0)
提交回复
热议问题