partial views code example

Example 1: change partial view based on select asp.net core

// partial view controller
public IViewResult LoadPartial() {
  return PartialView("_login"); // name of the partial view
}

// view (with jquery)
$("#container").load("LoadPartial");

Example 2: mvc render partial view in layout stackoverflow

//SERVER
  public ActionResult AdminMenu()
  {
      var am = _amr.GetAdminMenu();
      return Json(am,JsonBehaviour.AllowGet);
  }



  //CLIENT
    @using (Ajax.BeginForm("AdminMenu","AdminController", null,  
        new AjaxOptions
        {
            OnSuccess = "renderSuccess",
            OnFailure = "renderFailure",
            OnBegin = "renderBegin"
        },
        new
        {
            id = "frmViewerAdminMenu",
            name = "frmViewerAdminMenu"
        })
    )
    {
    ...

        <script type="text/javascript">
            function renderSuccess(ajaxContext){
               /// ajaxContext is whatever comes back from GetAdminMenu()
            }
        </script>
     ...
    }

Example 3: asp net core mvc partial view

<partial name="_PartialName" />

Example 4: c# mvc return partial view

// Create a container for your data
<div id="ViewHolder"><div>

//You can call your method using ajax:
$.ajax({
	type: "POST",
	url: '<Your path to your controller>/GetView',
	contentType: "application/text; charset=utf-8",
	dataType: "text",
	async: false,
	success: function (data) {
		// Populate your container
		$('ViewHolder').html(data);
	}
})

// In your controller 
public PartialViewResult GetView()
{
	//Passing a model is optional
  	MyModel myModel = new MyMyodel();
	return PartialView("<Your View Name>", myModel);
}