MVC4 Update Partial view

前端 未结 2 1238
太阳男子
太阳男子 2021-01-29 08:41

I am developing a simple MVC application.

I have main view, partial view and controller.

This is my main View

@model partitalViewTest.Models.Qset         


        
2条回答
  •  情话喂你
    2021-01-29 08:45

    Your button click is doing this:

    location.href='@Url.Action("CheckAnswer", "RazerQuestion", new { id = 'A' })    '"
    

    which navigates the browser to the partial page.

    I suspect what you want to happen is for the partial page content to be replaced with the updated content without refreshing the browser, or navigating away.

    You can do with with JQuery.

    First, give your an id:

    @Html.Partial("CheckAnswer",Model.partialModel)

    Second, do something similar to the following in response to the button click (error checking removed):

    function doWork(){
        $.get('@Url.Action("CheckAnswer", "RazerQuestion", new { id = 'A' }'), function (data) {
            $('#content').html(data);
        });
    }
    

    Finally, change the button to do this:

    
    

    This will now:

    • On click of the button...
    • Fire the doWork() function, which will...
    • Make the request to the CheckAnswer action, and then...
    • Take the content of the request, and replace the
      content with the result

提交回复
热议问题