Я пытаюсь передать параметр из поля зрения на контроллер,параметр перейти от View контролеру в осины 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 Routing:
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 }
);
Могу ли я знаю, что не так я делаю?
то, что вы делаете, совершенно неверно. вы не можете использовать onclick = "location.href = '@ Url.Action (" Index ", new {id = priceetails.PriceId})'", чтобы отправить на POST-метод –
У вас нет элементов управления в вашей форме! и почему вы делаете и отправку, и перенаправление? –
@frebin francis, спасибо, хорошо, я удалю форму, но как можно передать метод PriceId to Action – stom