我正在尝试通过jQuery发帖转到服务器,获取一个ID,然后检索到ID,让页面重定向到另一个控制器中的另一个动作。
这总是会返回404,所以我需要你的帮助。
我的行动:
public ActionResult SelectedVariation(string mealsAttribute, string portionsAttribute, string product)
{
Guid productId = new Guid(product);
CatalogManager catalogManager = CatalogManager.GetManager();
EcommerceManager ecommerceManager = EcommerceManager.GetManager();
ProductListWidgetModel model = new ProductListWidgetModel();
model.Product = catalogManager.GetProduct(productId);
model.ProductVariations = catalogManager.GetProductVariations(productId).ToList();
model.SelectedVariation = model.ProductVariations.Where(x => x.Variant.ToLower().Contains(mealsAttribute.ToLower()) && x.Variant.ToLower().Contains(portionsAttribute.ToLower())).FirstOrDefault().Id.ToString();
return RedirectToAction("Index", "RegisterOrderAccountForm", new { Id = model.SelectedVariation.ToString()});
//I've tried these ones aswell
//return Json(new { result = "Redirect", url = Url.Action("Index", "RegisterOrderAccountForm", new { Id = model.SelectedVariation.ToString()})}, JsonRequestBehavior.AllowGet);
// new RouteValueDictionary(new { controller = "RegisterOrderAccountForm", action = "Index", Id = model.SelectedVariation.ToString() })), JsonRequestBehavior.AllowGet);
}
我的View的JS代码:
function GetSelectedVariation() {
var meals = $('#MealsSelector').val();
var portions = $('#PortionsSelector').val();
$.getJSON('@Url.Action("SelectedVariation")', { mealsAttribute: $('#MealsSelector').val(), portionsAttribute: $('#PortionsSelector').val(), product: '@Model.Product.Id' }, function (route) {
window.location = route;
//Tried this aswell but to no avail
//window.location.href = getRootUrl() + route.RouteValues[1].Value + '/' + route.RouteValues[0].Value + '/' + route.RouteValues[2].Value;
});
}
总是得到404,我做错了什么?我注意到的另一件事是,在响应中,Id值不会与当前形式一起出现。
对某个操作进行ajax调用,然后重定向到另一个操作/控制器的正确方法是什么?
答案 0 :(得分:0)
返回对浏览器的HTTP 302响应,从而导致浏览器 向指定的操作发出GET请求。
我不认为您有兴趣获取HTML页面。
你应该返回一个JsonResult:
SELECT * FROM ws_campreplies
JOIN ws_campmsg on cr_phone = concat(cm_prefix, cm_phone)
因为我返回一个带有属性url的新json对象,你的ajax调用应该是:
public JsonResult SelectedVariation(string mealsAttribute, string portionsAttribute, string product)
{
return Json(new { url = Url.Action("RegisterOrderAccountForm", new { id = "1"}) }, JsonRequestBehavior.AllowGet);
}
答案 1 :(得分:0)
https://stackoverflow.com/a/12006362/2632619
操作应该返回如下格式的URL:
public ActionResult SelectedVariation(string mealsAttribute, string portionsAttribute, string product)
{
Guid productId = new Guid(product);
CatalogManager catalogManager = CatalogManager.GetManager();
EcommerceManager ecommerceManager = EcommerceManager.GetManager();
ProductListWidgetModel model = new ProductListWidgetModel();
model.Product = catalogManager.GetProduct(productId);
model.ProductVariations = catalogManager.GetProductVariations(productId).ToList();
model.SelectedVariation = model.ProductVariations.Where(x => x.Variant.ToLower().Contains(mealsAttribute.ToLower()) && x.Variant.ToLower().Contains(portionsAttribute.ToLower())).FirstOrDefault().Id.ToString();
// return RedirectToAction("Index", "RegisterOrderAccountForm", new { Id = model.SelectedVariation.ToString()});
var redirectUrl = new UrlHelper(Request.RequestContext).Action("Index", "RegisterOrderAccountForm", new { Id = model.SelectedVariation.ToString()});
return Json(new { Url = redirectUrl });
}
然后你的ajax调用应重定向如下:
$.getJSON('@Url.Action("SelectedVariation")', { mealsAttribute: $('#MealsSelector').val(), portionsAttribute: $('#PortionsSelector').val(), product: '@Model.Product.Id' }, function (route) {
window.location = route.Url;;
});
答案 2 :(得分:0)
虽然确实给出的大多数答案都是正确的,但事实证明问题出在Sitefinity itselft而不是真正的MVC或jQuery问题上。
虽然Telerik将Sitefinity作为企业级MVC CMS推向市场,但实际上Sitefinity的核心是使用WebForms构建的。
与MVC不同,Webforms有一个页面概念,内容在页面之间分开,因此简单地调用一个动作控制器是行不通的,因为它不知道动作控制器视图在哪个页面。
为了实现这一目标,我最终将我的ID存储在Session变量中,并且在从页面目录中获取目标页面后,我只是使用了一个简单的重定向。
所以,这是正确答案:
这是行动:
[HttpPost]
public ActionResult PlaceOrder()
{
string mealsAttribute = Request.Form["MealsSelector"].ToString();
string portionsAttribute = Request.Form["PortionsSelector"].ToString();
Guid productId = new Guid(Request.Form["ProductId"].ToString());
CatalogManager catalogManager = CatalogManager.GetManager();
EcommerceManager ecommerceManager = EcommerceManager.GetManager();
ProductListWidgetModel model = new ProductListWidgetModel();
model.Product = catalogManager.GetProduct(productId);
model.ProductVariations = catalogManager.GetProductVariations(productId).ToList();
model.SelectedVariation = model.ProductVariations.Where(x => x.Variant.ToLower().Contains(mealsAttribute.ToLower()) && x.Variant.ToLower().Contains(portionsAttribute.ToLower())).FirstOrDefault().Id.ToString();
var pages = GetLivePagesNativeAPI();
var page = pages.Where(x => x.NavigationNode.Title == "Subscribe");
string action = Url.Action("Index", "RegisterOrderAccountForm", new { Id = model.SelectedVariation.ToString() });
string destiny = FullyQualifiedApplicationPath + page.FirstOrDefault().UrlName.ToString();
Session["ProductListWidget_SelectedVariation"] = model.SelectedVariation;
return Redirect(destiny);
}
在我的视图中我放弃了jQuery Ajax POST并且我正在做一个简单的表单提交:
@using (Html.BeginFormSitefinity("PlaceOrder", "VariationSelector", FormMethod.Post))
{
<div class="bestillbox">
<input type="hidden" name="ProductId" value="@Model.Product.Id.ToString()" />
<table>
<tr>
<th id="row">@Html.DropDownList("MealsSelector", @Model.Meals, new { @onchange = "GetPortions();", Name="MealsSelector" })</th>
<td id="row">Middager</td>
</tr>
<tr>
<th id="row">@Html.DropDownList("PortionsSelector", @Model.Portions, new { @onchange = "GetPrice();", Name="PortionsSelector" })</th>
<td id="row">Porsjoner</td>
</tr>
</table>
<h6><span id="totalPrice"></span><span>kr/Uka</span></h6>
<input type="submit" class="bestillbutton" value="BESTILL"/>
</div>
}
希望这有助于任何人使用Sitefinity,并感谢大家的帮助。