R
R
Roman Gamretsky2014-05-27 17:47:30
PHP
Roman Gamretsky, 2014-05-27 17:47:30

How to dynamically replace PartialView in one page?

The situation is as follows: there are two PartialViews, one of which (PartialView1) has a form, after which the second PartialView2 should be displayed instead of the first one. Please tell me how to implement it.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alex Safonov, 2015-12-15
@elevenelven

https://jqueryui.com/autocomplete/

V
Vladislav Kilin, 2014-05-28
@rgamretsky

You can use the jquery.unobtrusive.ajax library that comes in the MVC 3+ box with the AjaxHelper helper. I would not recommend this solution for large web applications, since it requires you to have a horizontal dependency structure, which is not always convenient (and definitely inconvenient for modularity), but for small pages - just right, almost nothing needs to be written.

<div id="Container">
    @Html.Partial("Partial2")
</div>
@Ajax.ActionLink(
    "Заменить",
    "Replace",
    "Test",
    new { someId = 12 },
    new AjaxOptions{
        UpdateTargetId = "Container",
        InsertionMode = InsertionMode.Replace,
        HttpMethod = "POST"
    })

public class TestController : Controller
{
    [HttpPost]
    public ActionResult Replace(int someId)
    {
        return PartialView("Partial2", someId);
    } 
}

L
LChaos, 2014-05-28
@LChaos

You can for example like this, by clicking on the link (change to a button if necessary). The controller method must return a PartalView. The content of the div will be replaced with the resulting view

<div id="addnewcar">
    @Html.ActionLink("Добавить машину", "AddCar", null, new
                   {
                       id = "car"
                   })
</div>

$(document).ready(function () {
        $('#car').click(function (event) {
            event.preventDefault();
            var url = $(this).attr('href');
            $('#addnewcar').load(url);
        });
    });

B
Bob Smith, 2014-05-27
@bob_smith

Is the form being sent by ajax? If so, then return PartialView("PartialView2") in the controller method and change the form to the view received from the server with javascript.
If with a normal POST request for the entire page, then similarly, only return View("PartialView2") :-)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question