我试图提取两个变量,一个来自TextBox("search"),一个来自RadioButton("searchBy"),然后在单击ActionLink时将它们拉到控制器中。
当我使用带有submit按钮的搜索表单时," search“和"searchBy”字符串将被拉到控制器中。但是,如果我单击其中一个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>
}发布于 2015-03-06 16:47:13
@Html.ActionLink("Tekst", "Index", new {
sortOrder = ViewBag.TextSortParm,
onclick = "document.getElementById('<your form id>').submit();"
})不要忘记向表单中添加Id ()。
https://stackoverflow.com/questions/28902805
复制相似问题