在mvc4中使用jquery获取视图页面

时间:2022-08-24 22:09:23

Hi I am working with mvc4

你好,我在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,

当点击这个时,我正在使用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..

但美元。get(“Products/PlaceOrder”,函数(data) {});不打这个动作。

please help me.

请帮助我。

4 个解决方案

#1


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

这就是如何在Jquery get方法中传递数据到url的方法,注意在函数中使用了相同的参数名称。

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

#2


0  

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

确保你的URL是正确的。最有可能使用.action()。并使用new传递参数,如下所示。

    $.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();
    }

#3


0  

Just add HTTPGET attribute in your action method as below.

只需在操作方法中添加HTTPGET属性,如下所示。

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

java script

java脚本

$("#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


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:

当我希望我的视图代码像那样被获取,或者通过Html.Action()调用时,我使用了PartialView,并且通常将控制器操作设置为:

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

as an example:

作为一个例子:

TestViewModel

TestViewModel

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

PlaceOrder.cshtml

PlaceOrder.cshtml

@model TestViewModel

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

Index.html

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:

结果:

在mvc4中使用jquery获取视图页面

#1


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

这就是如何在Jquery get方法中传递数据到url的方法,注意在函数中使用了相同的参数名称。

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

#2


0  

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

确保你的URL是正确的。最有可能使用.action()。并使用new传递参数,如下所示。

    $.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();
    }

#3


0  

Just add HTTPGET attribute in your action method as below.

只需在操作方法中添加HTTPGET属性,如下所示。

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

java script

java脚本

$("#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


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:

当我希望我的视图代码像那样被获取,或者通过Html.Action()调用时,我使用了PartialView,并且通常将控制器操作设置为:

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

as an example:

作为一个例子:

TestViewModel

TestViewModel

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

PlaceOrder.cshtml

PlaceOrder.cshtml

@model TestViewModel

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

Index.html

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:

结果:

在mvc4中使用jquery获取视图页面