0

I have an MVC 2 application hosting on an IIS6 server. I have already done all the routing tweaks so that it can browse in the environment. The problem is however, that I have a dynamic partial view creation aspect, where a partial view is loaded each time an add button is clicked. Using Javascript and a controller, I call the partial vie and add it to a table each time.

Javascript Code

<script type="text/javascript">

$(function() {
    $("#btnAdd").click(function (e) {
        var itemIndex = $("#container input.iHidden").length;
        console.debug("itemIndex : "+itemIndex);
        e.preventDefault();
        var URL = "/WorkOrder/NewItem/" +itemIndex; 
        $.get(URL,function(data){
            $("#container").append(data);
        });           
    });
});

and the controller is

public ActionResult NewItem(int id)
    {
        var interest = new ItemModel { index = id };
        return View("_NewItem", interest);
    }

Quite simple really. The funny thing is that it works when in the test localhost environment, but as soon as i deploy it to production, the btnAdd function does nothing. After using the inspect element Network debugging tool, I discovered that the network is returning a 404 error for the partial view.

Do i have to tweak the routing tables more to make them recognize the routing regime i am trying to implement?

2
  • 1
    start simple. Is the javascript firing? pop an alert in there on the button click and go from there. Have you checked the console to see if any message are posting? Commented Sep 6, 2013 at 18:20
  • the javascript is firing. I went into the inspect element and monitored the network pane and it reports that the page cannot be found each time the button is clicked. I would have posted pictures, but I have not the rep for it Commented Sep 6, 2013 at 19:01

1 Answer 1

1

Try using Url.Action method instead of just hard coding the URI and pass the data using data parameter.

Example:

var URL = '<%= Url.Action("WorkOrder", "NewItem")%>';
$.get( URL,
       {id: itemIndex}
       function(data){
            $("#container").append(data);
       }); 
Sign up to request clarification or add additional context in comments.

5 Comments

ok, now the browser is tryign to parse the literal @Url. Action string.
No its doesn't...interestingly enough, if i manually type the url myserver/workOrder/NewItem/0, it loads the partial view.
lol my bad too, so used to using razor, I forgot for that moment that I was in MVC 2
ok, modified it, and that created a query string link which doesn't work either
Ok, so the problem is not the javascript of the code behind, but the routing. When i change the javascript link from /WorkOrder/NewItem/ to myserver/workOrder/NewItem/, it loads perfectly. Is there a more dynamic way for me to set the 'myservername' part of the link?

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.