Question

I have the following in my view:

@Html.DropDownList("ProductionOrder", null, htmlAttributes: new { @class = "form-control", @id = "ProductionOrder" })

<div class="col-lg-6" id="ProductionOrderDetails"></div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")

<script type="text/javascript">
    $(function () {
        $("#ProductionOrder").change(function () {
            var po = $("#ProductionOrder").val().toString();
            //This alert is for debug purpose only
            alert(po);
            $.get('/wetWashRequests/GetDetails/' + po, function (data) {
                $('#ProductionOrderDetails').html(data);
                $('#ProductionOrderDetails').fadeIn('fast');
            });
        })
    })
</script>

then I have the following in my controller:

public PartialViewResult GetDetails(string PONumber)
    {            
        var details = db.vwProductionOrderLookups.Where(x => x.No_ == PONumber).SingleOrDefault();
        return PartialView("_ProductionOrderDetails", details);
    }

What I don't understand is why it doesn't pass the value to the controller or why, when I enter the URL manually in the browser, like so(http://localhost:51702/wetWashRequests/GetDetails/WO033960), it also doesn't assign it to the parameter and so returns no data.

What am I missing? I thought I was on the right track but...

Was it helpful?

Solution 3

I think this modification will work:

$.get('/wetWashRequests/GetDetails?PONumber=' + po,

please note @malkam's remark to always use: @Url.Action(controller,action)

var url= "@Url.Action("wetWashRequests","GetDetails")"+"?PONumber="+po;

To clarify:

In your app-start you'll probably have the default routing:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", 
        id = UrlParameter.Optional }
    );

This means you can have URL's like:

/wetWashRequests/GetDetails/999

but then the 999 is bound to a parameter is called id.

For all other variables you'll need the

/wetWashRequests/GetDetails?someParameter=999

syntax.

Alternatively, you can modify your routing.

OTHER TIPS

You need to edit the route configuration to allow URL of type {controller}/{action}/{PONumber}. Otherwise, you can also send the PONumber via querystring, so that your URL looks like this:

http://localhost:51702/wetWashRequests/GetDetails?PONumber=WO033960

Use URL.Action() method

var url= "@Url.Action("wetWashRequests","GetDetails")"+"?PONumber="+po;
$.get(url,function(data)
{
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top