我试图拉出两个变量,一个来自TextBox(“搜索”),另一个来自RadioButton(“searchBy”),并在单击ActionLink时将它们拉入控制器。
当我使用带有提交按钮的搜索表单时,“搜索”和“搜索”字符串被拉入控制器。但如果我单击其中一个ActionLinks,我只会得到“sortOrder”字符串。我认为这是因为ActionLink没有提交?
所以我认为有两种方法可以解决它,因此我的两个问题是:
我的家庭控制器:
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>
}
答案 0 :(得分:0)
1。 您可以在操作链接中使用JavaScript来执行提交:
@Html.ActionLink("Tekst", "Index", new {
sortOrder = ViewBag.TextSortParm,
onclick = "document.getElementById('<your form id>').submit();"
})
不要忘记在表单中添加Id()。