2

Hi I am working with mvc4

I have a razor view page for the action

  public ActionResult DeliveryAddress(string userid,int productid)
    {

       ....
        return View(m);
    }

that contain

<div ><a href="" class="btn btn-primary" id="place-order">DELIVER HERE</a></div>

when clicking on this i am collecting somedata ifrom this page using jquery,

$(document).ready(function () {   
$("#place-order").click(function () {
    var userid = $('#selected-userId').html();
    var productid = $('#selected-productId').html();


   $.get("Products/PlaceOrder/"+ userid, function (data) { });
});

});

and i want to pen another view of action

 [HttpGet]
    public ActionResult PlaceOrder(int uid)
    {
        return View();
    }

and paste the variable content,

but $.get("Products/PlaceOrder", function (data) { }); is not hitting this action..

please help me.

3
  • Lot of details seems to be missing, how u passing data to the url? Commented Jan 2, 2015 at 7:08
  • @ssilas777:currently i just want to hit the controller action thats why i write the code like that Commented Jan 2, 2015 at 7:16
  • But unless you provided all details, no one can nail the actual issue, the way you pass the parameters may even cause the issue rit. Commented Jan 2, 2015 at 7:18

4 Answers 4

1

This is how you need to pass a data to a url in Jquery get method, note the same parameter name is used in the function

  $.get('@Url.Action("PlaceOrder","Products")', { uid: userid }, function (data)
  { 
  });
Sign up to request clarification or add additional context in comments.

Comments

0

Make sure your URL is correct. Most probably use @Url.Action(). and also pass the parameter using new as shown below.

    $.get('@Url.Action("PlaceOrder","Products",new { userid  =  @userid  , productid  =  @productid   })', function (data) { 

    });

While collecting the data make sure your parameter names are same for both while sending and while receiving.

    [HttpGet]
    public ActionResult PlaceOrder(int userid, int productid )
    {
    return View();
    }

Comments

0

Just add HTTPGET attribute in your action method as below.

[HttpGet]
public ActionResult PlaceOrder()
{
    return View();
}

java script

$("#place-order").click(function () {
    var userid = $('#selected-userId').html(); // $('#selected-userId').val();

    $.get('@Url.Action("PlaceOrder","Products", new { uid = userid })', function (data) { });

    var productid = $('#selected-productId').html();
});

4 Comments

Did you check with alert for. to check is that place-order on click is triggering or not like this $("#place-order").click(function () { alert('OK'); });
Try like the update code now. HTTPGET attribute is must in you action method.
Try now Please, Surely it will helps
$('#selected-userId').html(); is wrong i thing, may be it should be $('#selected-userId').val(); . Please check its value with alert.
0

When I want my view code to be fetched like that, or even through the Html.Action() call, I use the PartialView and normally set my Controller Action as:

public ActionResult PlaceOrder(int uid)
{
    return PartialView(new TestViewModel() { ID = uid });
}

as an example:

TestViewModel

public class TestViewModel
{
    public int ID { get; set; }
}

PlaceOrder.cshtml

@model TestViewModel

<h2>Partial View</h2>
<p>
    Partial View paragraph with the id <b>@Model.ID</b>
</p>

Index.html

<hr />

@Html.Action("PartialView", "Home", new { id = 44 })

<hr />

<div class="ap"></div>

<script>
    var url = '@Url.Action("PartialView", "Home")';
    $.get(url, { id: 54 }, function (data) {
        $(".ap").append(data);
    });
</script>

result:

enter image description here

Comments

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.