views:

270

answers:

2

i have aspx page which has following js function which is called on button click

enter code here


<input type="button" onclick="calltemp1()" value="Temp1"/>


    <script type="text/javascript">
        function calltemp1() {

          $("#Renderthisdiv").load("/Views/Templates/_Temp1.ascx");
         }             
    </script>


my _Temp1.ascx page renders another page Temp1.ascx my _Temp1.ascx contains

enter code here
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

<div>
<%Html.RenderPartial("/Views/Templates/Temp1.ascx"); %>
</div>

when i run the program i get the JavaScript runtime error saying "object expected" please help me to solve this issue

A: 

Just add the appropriate action to your controller so you can use jQuery to render it:

public class TemplateController : Controller {
 public ViewResult Temp1() {
  return View("_Temp1")
 }
}
Dmytrii Nagirniak
+1  A: 

Your JavaScript call is going to make another trip through the MVC Pipeline. So it will hit routing, a controller, and then a view. Your JavaScript should not try to hit the ascx file directly, but a route that maps to a controller that renders the view.

Your JS should look like this (note this is using a root relative URL, you may have to adjust):

$("#Renderthisdiv").load("/template/temp1");

Alternately, you can use an HTML helper to get the URL, but the JS will have to be in your view:

$("#Renderthisdiv").load("<%= Html.Action("temp1", "template") %>");

That URL will hit the Temp1 action on the TemplateController

public class TemplateController : Controller {
    public ViewResult Temp1() {
            return View("Temp1");
    }
}
Lance Fisher