我无法将视图代码中的参数str传递给我的控制器。它来自null。我想让它传递为""。
任何帮助?
控制器代码:
public ActionResult Index(string str, string sortOrder)
{
if (str == "")
return View(db.Ports.OrderByDescending(model => model.PurchaseDate));
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
var investments = from s in context.Investments
select s;
switch (sortOrder)
{
case "name_desc":
investments = investments.OrderByDescending(s => s.LastName);
break;
case "Date":
investments = investments.OrderBy(s => s.PurchaseDate);
break;
case "date_desc":
investments = investments.OrderByDescending(s => s.PurchaseDate);
break;
default:
investments = investments.OrderBy(s => s.LastName);
break;
}
return View(investments.ToList());
}
查看代码:
@model StockHoldings.Models.Investments
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js" type='text/javascript'></script>
@*<script> src='http://code.jquery.com/jquery-latest.min.js' </script>*@
</head>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Investments</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.LastName, (SelectList)ViewBag.LastNames)
@*@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })*@
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Fund, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Fund, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Fund, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PurchaseDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PurchaseDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PurchaseDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Shares, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.Shares, new { htmlAttributes = new { @class = "form-control" } })*@
@Html.TextBoxFor(model => model.Shares, new { id = "sharesID", name = "Shares", htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Shares, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PurchasePrice, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.PurchasePrice, new { htmlAttributes = new { @class = "form-control" } })*@
@Html.TextBoxFor(model => model.PurchasePrice, new { id = "purchasepriceID", name = "PurchasePrice", htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PurchasePrice, "", new { @class = "text-danger" })
</div>
</div>
@*<div class="form-group">
@Html.LabelFor(model => model.PurchaseAmount, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.PurchaseAmount, new { id = "purchaseamountID", name = "PurchaseAmount", htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PurchaseAmount, "", new { @class = "text-danger" })
</div>
</div>
*@
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
这是链接:
@Html.ActionLink("Investments", "Index", "Investments", new { str = "", sortOrder = "Date" }, null)
</div>
答案 0 :(得分:0)
您可以更改操作中的参数方法使字符串str为可选参数以清空。该工作 public ActionResult Index(string sortOrder,string str =“”)