1

In our ASP.NET MVC 4 app, we have four partial views: View1, View2, View3, View4. When user selects a number, say, 2, from a dropdown and click on a submit button we want to render only partial views View2. Likewise, when 3 is selected we want to render View3 and so on. I tried the following but it does not render View2. I assume we cannot do this from inside script tag.

<div id="v2">
    <script>
        if (n==2) {
            @{Html.RenderPartial("View2");}
        }
    </script>
</div>

How can we achieve the above?

1 Answer 1

2

Load it asynchronously using jQuery load method on the click event of the button. Something like this.

$(function(){

  $(document).on("click","#btnItem1",function() {
     var val=$("#YourDropDownID").val();     
     if(val==2)
     {
        $("#DivContainerForCustForm").load("@Url.Action("CustomerForm","Home")");
     }
  });  

});

Assuming you have the CustomerForm action method in HomeController returns the relevant html markup you need.This action could return a partial view.

You may update your jQuery selectors & HTML markup to make it more generic to work with more than one control/div's.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks to @Shyju for his help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.