在 asp mvc 中将参数从 View 传递到 Controller
pass parameter from View to Controller in asp mvc
我正在尝试将参数从视图传递到控制器,
这是我的观点:
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@foreach (var pricedetails in ViewBag.PriceTotal)
{
<div style="text-align:center; clear:both ">
<h5 class="product-title">@pricedetails.Title</h5>
</div>
<div class="product-desciption" style="height:40px">@pricedetails.Descriptions</div>
<p class="product-desciption product-old-price"> @pricedetails.PricePoints</p>
<div class="product-meta">
<ul class="product-price-list">
<li>
<span class="product-price">@pricedetails.PricePoints</span>
</li>
<li>
<span class="product-save">Get This Free</span>
</li>
</ul>
<ul class="product-actions-list">
<input type="submit" name='giftid' value="Get Gift"
onclick="location.href='@Url.Action("Index", new { id = pricedetails.PriceId })'" />
</ul>
</div>
}
}
我的操作方法:
提交后到达操作方法,但我无法获得每个价格的 PriceId
[HttpPost]
public ActionResult Index(int id=0) // here PriceId is not passed on submit
{
List<Price_T> priceimg = (from x in dbpoints.Price_T
select x).Take(3).ToList(); ;
ViewBag.PriceTotal = priceimg;
var allpoint = singletotal.AsEnumerable().Sum(a => a.Points);
var price = from x in dbpoints.Price_T
where x.PriceId == id
select x.PricePoints;
int pricepoint = price.FirstOrDefault();
if (allpoint < pricepoint)
{
return Content("<script language='javascript' type='text/javascript'>alert('You are not elgible');</script>");
}
else
{
return Content("<script language='javascript' type='text/javascript'>alert('You are elgible');</script>");
}
return View("Index");
}
Url路由:
routes.MapRoute(
name: "homeas",
url: "Index/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
请问我做错了什么?
我认为你应该在这里更正
<input type="button" name='giftid' value="Get Gift"
onclick="location.href='@Url.Action("Index", new { id = pricedetails.PriceId })'" />
我建议你去掉 Html.BeginForm()
。只需保留 for...loop
并像这样定义 "Get Gift" 按钮:
<input type="button" id='giftid' name='giftid' value="Get Gift" onclick="getGift(@(pricedetails.PriceId))'" />
然后,在Get Gift
按钮所在的视图文件底部,定义一些JavaScript:
<script type="text/javascript">
function getGift(priceId) {
$.ajax({
type: 'GET',
url: '@Url.Action("Index", "Home")',
data: { priceId: priceId },
contentType : "json",
success:function(data){
// Do whatever in the success.
},
error:function(){
// Do whatever in the error.
}
});
</script>
通过使用 ajax 调用获取礼物数据,您无需提交任何内容。这使您的情况变得容易得多。按下 Get Gift
按钮只会发出 ajax 呼叫。
我没有时间亲自尝试,但希望上面的例子能让你振作起来 运行。
编辑:
我花了一些时间想出了一个例子。
Controller
public class HomeController : Controller
{
public ActionResult Index()
{
var items = new List<int>();
items.Add(1);
items.Add(2);
items.Add(3);
return View(items);
}
public ActionResult GetGift(int priceId)
{
return RedirectToAction("Index"); // You'll be returning something else.
}
}
查看
@model List<int>
@foreach (var price in Model)
{
<input type="button" id='giftid' name='giftid' value="Get Gift" onclick="getGift(@(price))" />
}
<script type="text/javascript">
function getGift(priceId) {
$.ajax({
type: 'GET',
url: '@Url.Action("GetGift", "Home")',
data: { priceId: priceId },
contentType: "json",
success: function(data) {
// Do whatever in the success.
},
error: function() {
// Do whatever in the error.
}
});
}
</script>
希望对您有所帮助。
请在您的 cshtml 页面中使用以下内容
@foreach (var item in Model)
{
@Html.ActionLink(item.PriceDetails, "GetGift", new { priceID = item.priceID }, new { @class = "lnkGetGift" })
}
<script type="text/javascript" src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("a.lnkGetGift").on("click", function (event) {
event.preventDefault();
$.get($(this).attr("href"), function (isEligible) {
if (isEligible) {
alert('eligible messsage');
}
else
{
alert('not eligible messsage');
}
})
});
});
</script>
并在控制器中
[HttpGet]
public JsonResult GetGift(int priceID)
{
List<Price_T> priceimg = (from x in dbpoints.Price_T
select x).Take(3).ToList(); ;
ViewBag.PriceTotal = priceimg;
var allpoint = singletotal.AsEnumerable().Sum(a => a.Points);
var price = from x in dbpoints.Price_T
where x.PriceId == id
select x.PricePoints;
int pricepoint = price.FirstOrDefault();
if (allpoint < pricepoint)
{
return Json(false, JsonRequestBehavior.AllowGet);
}
else
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}
请根据方法参数和价格实体更改参数
希望这有帮助。
我正在尝试将参数从视图传递到控制器,
这是我的观点:
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@foreach (var pricedetails in ViewBag.PriceTotal)
{
<div style="text-align:center; clear:both ">
<h5 class="product-title">@pricedetails.Title</h5>
</div>
<div class="product-desciption" style="height:40px">@pricedetails.Descriptions</div>
<p class="product-desciption product-old-price"> @pricedetails.PricePoints</p>
<div class="product-meta">
<ul class="product-price-list">
<li>
<span class="product-price">@pricedetails.PricePoints</span>
</li>
<li>
<span class="product-save">Get This Free</span>
</li>
</ul>
<ul class="product-actions-list">
<input type="submit" name='giftid' value="Get Gift"
onclick="location.href='@Url.Action("Index", new { id = pricedetails.PriceId })'" />
</ul>
</div>
}
}
我的操作方法:
提交后到达操作方法,但我无法获得每个价格的 PriceId
[HttpPost]
public ActionResult Index(int id=0) // here PriceId is not passed on submit
{
List<Price_T> priceimg = (from x in dbpoints.Price_T
select x).Take(3).ToList(); ;
ViewBag.PriceTotal = priceimg;
var allpoint = singletotal.AsEnumerable().Sum(a => a.Points);
var price = from x in dbpoints.Price_T
where x.PriceId == id
select x.PricePoints;
int pricepoint = price.FirstOrDefault();
if (allpoint < pricepoint)
{
return Content("<script language='javascript' type='text/javascript'>alert('You are not elgible');</script>");
}
else
{
return Content("<script language='javascript' type='text/javascript'>alert('You are elgible');</script>");
}
return View("Index");
}
Url路由:
routes.MapRoute(
name: "homeas",
url: "Index/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
请问我做错了什么?
我认为你应该在这里更正
<input type="button" name='giftid' value="Get Gift"
onclick="location.href='@Url.Action("Index", new { id = pricedetails.PriceId })'" />
我建议你去掉 Html.BeginForm()
。只需保留 for...loop
并像这样定义 "Get Gift" 按钮:
<input type="button" id='giftid' name='giftid' value="Get Gift" onclick="getGift(@(pricedetails.PriceId))'" />
然后,在Get Gift
按钮所在的视图文件底部,定义一些JavaScript:
<script type="text/javascript">
function getGift(priceId) {
$.ajax({
type: 'GET',
url: '@Url.Action("Index", "Home")',
data: { priceId: priceId },
contentType : "json",
success:function(data){
// Do whatever in the success.
},
error:function(){
// Do whatever in the error.
}
});
</script>
通过使用 ajax 调用获取礼物数据,您无需提交任何内容。这使您的情况变得容易得多。按下 Get Gift
按钮只会发出 ajax 呼叫。
我没有时间亲自尝试,但希望上面的例子能让你振作起来 运行。
编辑:
我花了一些时间想出了一个例子。
Controller
public class HomeController : Controller
{
public ActionResult Index()
{
var items = new List<int>();
items.Add(1);
items.Add(2);
items.Add(3);
return View(items);
}
public ActionResult GetGift(int priceId)
{
return RedirectToAction("Index"); // You'll be returning something else.
}
}
查看
@model List<int>
@foreach (var price in Model)
{
<input type="button" id='giftid' name='giftid' value="Get Gift" onclick="getGift(@(price))" />
}
<script type="text/javascript">
function getGift(priceId) {
$.ajax({
type: 'GET',
url: '@Url.Action("GetGift", "Home")',
data: { priceId: priceId },
contentType: "json",
success: function(data) {
// Do whatever in the success.
},
error: function() {
// Do whatever in the error.
}
});
}
</script>
希望对您有所帮助。
请在您的 cshtml 页面中使用以下内容
@foreach (var item in Model)
{
@Html.ActionLink(item.PriceDetails, "GetGift", new { priceID = item.priceID }, new { @class = "lnkGetGift" })
}
<script type="text/javascript" src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("a.lnkGetGift").on("click", function (event) {
event.preventDefault();
$.get($(this).attr("href"), function (isEligible) {
if (isEligible) {
alert('eligible messsage');
}
else
{
alert('not eligible messsage');
}
})
});
});
</script>
并在控制器中
[HttpGet]
public JsonResult GetGift(int priceID)
{
List<Price_T> priceimg = (from x in dbpoints.Price_T
select x).Take(3).ToList(); ;
ViewBag.PriceTotal = priceimg;
var allpoint = singletotal.AsEnumerable().Sum(a => a.Points);
var price = from x in dbpoints.Price_T
where x.PriceId == id
select x.PricePoints;
int pricepoint = price.FirstOrDefault();
if (allpoint < pricepoint)
{
return Json(false, JsonRequestBehavior.AllowGet);
}
else
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}
请根据方法参数和价格实体更改参数 希望这有帮助。