无法呈现按钮点击视图
当点击“a”锚标记我想从一个控制器(HomeController.cs)重定向到另一个控制器(CartController.cs)索引[GET]并执行代码并返回数据到视图(车/ index.cshtml)。无法呈现按钮点击视图
这里是js代码
$(document).on('click', '.btn-margin', function() {
if (parseInt($('#UserID').val()) > 0) {
var pmID = $(this).attr("id"),
bid = $(this).attr("brand-id");
$.ajax({
url: '@Url.Action("index", "cart")',
data: { "id": pmID, "bid" : bid },
type: 'GET',
dataType: 'json',
success: function (response) {
},
error: function (xhr, status, error) {
}
});
}
});
和CartController
[HttpGet]
public ActionResult Index(long id = 0, long bid = 0)
{
GetStates();
return View(productBL.GetProductById(id, bid));
}
正如预期的那样具有重定向到的车index.cshtml ..但我的结果仍然在指数的HomeController。 cshtml页面。
请帮我如何获得预期的结果..
你不需要Ajax调用此。 on you'a'点击使用类似这样的东西
$('.btn-margin').on('click',function(){
if (parseInt($('#UserID').val()) > 0) {
var pmID = $(this).attr("id"),
var bid = $(this).attr("brand-id");
window.location.href = "/cart/index?id="+pmID+"&bid=" +bid;
}
}
希望这会有所帮助。
这显示了url中的参数..我试图通过ajax-call来避免这.. .. @ karthik – thiru
@thiru如果你有类或id容器在你的视图呈现..然后成功,你可以做到这一点$(“id或类的容器”)。html(响应) –
在你的AJAX调用,你定义了这个:
$.ajax({
dataType: 'json',
但是你的控制器动作返回HTML,而不是JSON:
public ActionResult Index(long id = 0, long bid = 0)
return View(productBL.GetProductById(id, bid));
它应该返回使用JSON方法的数据:
return Json(prodcutBL.GetProductById(id, bid), JsonBehavior.AllowGet);
第二个参数表示允许GET请求(通常POST只是必需的,否则woul d抛出异常)。这将返回一个JSON对象到成功回调,然后你可以像正常一样访问数据。您可能要直接返回一个对象,而不是数组,如:
return Json(new { products = prodcutBL.GetProductById(id, bid) }, JsonBehavior.AllowGet);
,然后在回调访问它想:
success: function (response) {
if (response.products.length == 0)
alert("No data available");
else /* do something */
},
Microsoft建议returning an object, not an array, for a web response.
试试这个 - return RedirectToAction("Index", "CartController", new{ id: pmId})
您是否尝试添加返回RedirectToAction(“Index”,“CartController”);在你的家庭控制器? – ISHIDA
我需要将参数传递给该动作 – thiru
试试这个 - 返回RedirectToAction(“Index”,“CartController”,new {id:pmId}) – ISHIDA