【问题标题】:MVC, Pull TextBox and RadioButton variable with ActionLinkMVC,使用 ActionLink 拉取 TextBox 和 RadioButton 变量
【发布时间】:2015-03-10 19:05:22
【问题描述】:

我正在尝试提取两个变量,一个来自 TextBox("search"),另一个来自 RadioButton("searchBy"),并在单击 ActionLink 时将它们拉入控制器。

当我使用带有提交按钮的搜索表单时,“search”和“searchBy”字符串被拉入控制器。但是,如果我单击其中一个 ActionLink,我只会得到“sortOrder”字符串。我认为这是因为 ActionLink 没有提交?

所以我认为有两种方法可以解决它,因此我有两个问题:

  1. 是否可以让 ActionLink 对点击的表单执行提交?
  2. 是否可以使用 ActionLink 提取“search”和“searchBy”字符串?

我的 HOME 控制器:

 public ActionResult Index(string searchBy, string search, string sortOrder)
    {
        ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
        ViewBag.TextSortParm = String.IsNullOrEmpty(sortOrder) ? "text_desc" : "";
        ViewBag.PriceSortParm = sortOrder == "Price" ? "price_desc" : "Price";
        ViewBag.CubicMeterSortParm = sortOrder == "CubicMeter" ? "cubicMeter_desc" : "CubicMeter";
        ViewBag.PricePerCubicSortParm = sortOrder == "PricePerCubic" ? "pricePerCubic_desc" : "PricePerCubic";
        ViewBag.ConstructionTypeSortParm = sortOrder == "constructionType" ? "constructionType_desc" : "constructionType";

        var text = from s in db.LearningNumbers select s;

        if (!String.IsNullOrEmpty(search))
        {
            if (searchBy == "Tekst")
            {
                text = text.Where(x => x.Note.Contains(search) || search == null);
            }
            else
            {
                text = text.Where(x => x.Name.Contains(search) || search == null);
            }
        }

        switch (sortOrder)
        {
            case "name_desc":
                if (searchBy == "Tekst")
                {
                    text = text.OrderByDescending(s => s.Name).Where(x => x.Note.Contains(search) || search == null);
                }
                else
                {
                    text = text.OrderByDescending(s => s.Name).Where(x => x.Name.Contains(search) || search == null);
                }
                break;
            case "text_desc":
                if (searchBy == "Tekst")
                {
                    text = text.OrderByDescending(s => s.Note).Where(x => x.Note.Contains(search) || search == null);
                }
                else
                {
                    text = text.OrderByDescending(s => s.Note).Where(x => x.Name.Contains(search) || search == null);
                }
        }
        return View(text.ToList());
    }

我的索引视图:

 @using (Html.BeginForm("Index", "Home", FormMethod.Get, new { id = "searchForm" }))
    {
<div class="row">
<div class="col-md-12">
    <hr />
    <h2>Søgeresultater</h2>
        @Html.RadioButton("searchBy", "Navn", true) <text>Navn</text>
        @Html.RadioButton("searchBy", "Tekst") <text>Tekst</text><br />
        @Html.TextBox("search")<input class="btn btn-primary" style="margin:0 10px;" type="submit" value="Søg" />

</div>

<div class="col-md-12 table" style="display:table; margin:45px 0 25px 0;">
    <div class="col-md-2">
        @*<b>Tekst</b><br />*@
        <h4>@Html.ActionLink("Navn", "Index", new { sortOrder = ViewBag.NameSortParm, onclick = "document.getElementById('searchForm').submit();" } })</h4>
    </div>
    <div class="col-md-2">
        @*<b>Tekst</b><br />*@
        <h4>
            @Html.ActionLink("Tekst", "Index", new { sortOrder = ViewBag.TextSortParm, onclick = "document.getElementById('searchForm').submit();" } })
        </h4>
    </div>
</div>
</div>
}

【问题讨论】:

  • 您使用的是哪个版本的 MVC?另外,我假设 search 和 searchBy 应该由用户输入填充(例如在文本框中输入的内容)?如果是这样,请查看hereherehere 了解更多信息。
  • 感谢您的回答。我正在使用 MVC5,您的假设是正确的。 search 是一个文本框,searchBy 是 Radiobuttons。谢谢!我会看看你链接到的解决方案。
  • 我已经测试了您链接中的每个建议,但不幸的是它们似乎不起作用。

标签: c# model-view-controller actionlink


【解决方案1】:

1。 您可以在操作链接中使用 JavaScript 来执行提交:

@Html.ActionLink("Tekst", "Index", new { 
     sortOrder = ViewBag.TextSortParm,  
     onclick = "document.getElementById('<your form id>').submit();"
})

不要忘记在表单中添加一个 Id ()。

  1. 没有。

【讨论】:

  • 嗨 Yann,抱歉回复晚了!我尝试按照您提到的方式进行设置,但我的搜索字符串仍然为空。我已经编辑了我的问题,因此它包含您的解决方案。我错过了什么吗?
猜你喜欢
  • 2011-05-25
  • 1970-01-01
  • 2017-12-08
  • 1970-01-01
  • 2013-10-21
  • 2018-09-19
  • 1970-01-01
  • 2015-03-12
  • 1970-01-01
相关资源
最近更新 更多