2012-04-24 73 views
0

我有一个比较页面,基本上做产品之间的比较。比较页面用beginform包装起来,并且在表单中至少有比较按钮和选择按钮,这些按钮将出现在每个列出的产品中。一个按钮指向不同的控制器在MVC beginform内

顾名思义,比较按钮将加载另一页来比较所选产品。和选择按钮会将选定的产品添加到购物车。

任何想法如何单独比较按钮,并选择按钮之间的行动?

回答

1

这可以用两种方式来完成我认为。

使用Html.ActionLink作为比较按钮来调用另一个方法/控制器。与此类似,只是你的形式将不会公布,所以你需要的东西传入参数:

@using(Html.BeginForm("SomeController","Select")) 
{ 
<button name="selectBtn" value="select">Select</button> 
@Html.ActionLink("Click to Compare", "SomeOtherController", "Compare" 
    new { id1=xx, id2=yy },//pass parameters accordingly 
    null 
) 
} 

我用ActionLink的方法this overload

OR

如果你真的在这里需要一个按钮,你可以有两个按钮和检测哪个按钮被称为控制器和重定向。

@using(Html.BeginForm("CommonController","Select")) 
{ 
<button name="button" value="select">Select</button> 
<button name="button" value="compare">Compare</button> 
} 

您控制器:

public class CommonController : Controller 
{ 
public ActionResult Select(string button)//same as the name property of <button> 
{ 
    if(button == "compare") 
    {//do something if you need to 
     //you could again pass in data as part of the route here.. 
    return RedirectToAction("SomeController", "Compare"); 
    } 
} 
} 
0

这是HTML的限制,您无法在单个表单元素中发布到其他网址。你将需要使用JavaScript来实现这一点。在点击你的按钮时,你需要调用一个javascript方法,并设置表单动作url来指向比较或选择url。像这样的东西

function OnButton1() 
{ 
    document.Form1.action = @url.Action(new {Controller = ctrl1, Action = action }); 

    document.Form1.submit();    // Submit the page 

    return true; 
} 

function OnButton2() 
{ 
    document.Form1.action = @url.Action(new {Controller = ctrl2, Action = action }); 

    document.Form1.submit();    // Submit the page 

    return true; 
} 
+0

但不会提交表单收集。 – user384080 2012-04-24 06:46:24

+0

假设表单名为Form1并且它有一些类型的复选框,它会提交这些值 – Chandermani 2012-04-24 08:32:58

相关问题