如何通过MVC中的按钮单击将参数传递给Controller?

时间:2012-09-17 11:50:48

标签: asp.net-mvc asp.net-mvc-3 razor devexpress

有人可以帮我解决如何将我的参数从组合框中绑定到我控制器中某个动作的参数上。我正在使用DevExpress控件套件,这是我的代码:

 [HttpPost]
 public ActionResult Filter(string filterCriteria)
 {
     ViewData.Model = Repository.GetReports(filterCriteria);
     return RedirectToAction("Index");
 }

这是我的观点:

<div style="float:right;padding-bottom:20px;" >
@using (Html.BeginForm())
{
<table><tr><td>
Filter by Keyword:</td>
<td>
@Html.DevExpress().DropDownEdit(
    settings =>
    {
        settings.Name = "checkComboBox";
        settings.Width = 210;
        settings.Theme = Utils.CurrentTheme;
        settings.Properties.DropDownWindowStyle.BackColor = System.Drawing.Color.FromArgb(0xEDEDED);
        settings.Properties.ClientSideEvents.ValueChanged = "function(s, e) {e.customArgs[\"filterCriteria\"] = checkComboBox.Name.toString();}";

        settings.SetDropDownWindowTemplateContent(c =>
        {
            @Html.DevExpress().ListBox(
                listBoxSettings =>
                {
                    listBoxSettings.Name = "checkListBox";
                    listBoxSettings.SkinID = "CheckComboBoxListBox";
                    listBoxSettings.CallbackRouteValues = new { Controller = "Report", Action = "Filter" };
                    listBoxSettings.Theme = Utils.CurrentTheme;
                    listBoxSettings.ControlStyle.Border.BorderWidth = 2;
                    listBoxSettings.ControlStyle.BorderBottom.BorderWidth = 1;
                    listBoxSettings.ControlStyle.BorderBottom.BorderColor = System.Drawing.Color.FromArgb(0xDCDCDC);
                    listBoxSettings.Width = System.Web.UI.WebControls.Unit.Percentage(100);
                    listBoxSettings.Height = 100;

                    listBoxSettings.Properties.SelectionMode = ListEditSelectionMode.CheckColumn;

                    listBoxSettings.Properties.ClientSideEvents.SelectedIndexChanged = "OnListBoxSelectionChanged";
                }
            ).BindList(Repository.GetKeywords())
            .Render();
            ViewContext.Writer.Write("<table style=\"width:100%\"><tr><td align=\"right\">");
            @Html.DevExpress().Button(
                buttonSettings =>
                {
                    buttonSettings.Name = "buttonClose";
                    buttonSettings.Text = "Close";
                    buttonSettings.Style.Add("float", "right");
                    buttonSettings.Theme = Utils.CurrentTheme;
                    buttonSettings.ClientSideEvents.Click = "function(s, e){ checkComboBox.HideDropDown(); }";
                }
            )
            .Render();
            ViewContext.Writer.Write("</td></tr></table>");
        });
        settings.Properties.EnableAnimation = true;
        settings.Properties.ClientSideEvents.TextChanged = "SynchronizeListBoxValues";
        settings.Properties.ClientSideEvents.DropDown = "SynchronizeListBoxValues";
    }
).GetHtml()
</td><td>
@Html.DevExpress().Button(
    settings =>
    {
        settings.Name = "btnApply";
        settings.ControlStyle.CssClass = "button";
        settings.Text = "Apply";
        settings.UseSubmitBehavior = true;
        settings.Theme = Utils.CurrentTheme;
        settings.Width = 80;
        settings.RouteValues = new { Controller = "Report", Action = "Filter" };
    }
).GetHtml()
</td><td>
@Html.DevExpress().Button(
    settings =>
    {
        settings.Name = "btnClear";
        settings.ControlStyle.CssClass = "button";
        settings.Text = "Clear";
        settings.ClientSideEvents.Click = "function(s, e){ ASPxClientEdit.ClearEditorsInContainer(); }";
        settings.Theme = Utils.CurrentTheme;
        settings.Width = 80;
    }
).GetHtml()
</td></tr></table>
}
</div>

我基本上在底部有一个gridview,我想用它上面的下拉列表进行过滤。我通过使用按钮来过滤我的集合来调用该操作。在这个例子中,我只是试图传回控件的名称,即

settings.Properties.ClientSideEvents.ValueChanged = "function(s, e) {e.customArgs[\"filterCriteria\"] = checkComboBox.Name.toString();}";

只是为了看看我是否能够传回任何值,但这失败了。有人可以详细说明一下这样做吗?谢谢,

2 个答案:

答案 0 :(得分:2)

从我收集的内容中,您想要检索Controller侧的“checkListBox”ListBox中选择的多个项目。 如果是这样,请按以下方式使用ListBoxExtension.GetSelectedValues方法:

[HttpPost]
public ActionResult Filter(string filterCriteria)
{
    //listBoxSettings.Name = "checkListBox";

    string[] values = ListBoxExtension.GetSelectedValues<string>("checkListBox");
    filterCriteria = values...

    ViewData.Model = Repository.GetReports(filterCriteria);
    return RedirectToAction("Index");
}

答案 1 :(得分:0)

我知道这是一个较旧的问题,但我想我可能会添加viewbag的使用,它允许您将数据从控制器发送到视图,反之亦然。以下是mdsn的示例。

<强>控制器

public ActionResult Index()
{
    ViewBag.MyMessageToUsers = "Hello from me.";
    ViewBag.AnswerText = "Your answer goes here.";

    return View();
}

查看

@ViewBag.MyMessageToUsers
@Html.TextBox("AnswerText")