尝试将模型绑定到视图时,我收到404错误。 ActionResult被称为“SortDesc”,但视图只是一个标准的“索引”视图。我知道这不是加载,因为现在有一个名为'SortDesc'的视图。但是我有两个按钮,一个用于对数据进行排序,另一个用于对数据进行排序,从而降低两个独立的功能。我只是不确定这里最好的解决方案是什么,要么继续使用两个单独的函数,要么以某种方式传递正确的视图来加载或在Index上创建一个新的单个HttpPost函数,该函数将知道哪个按钮已被点击并相应地排序。这是我到目前为止的代码:
型号:
public class NumberSetList
{
public int NumberSetListId { get; set; }
public List<NumberList> Numbers { get; set; }
public string SortOrder { get; set; }
}
public class NumberList
{
public int NumberListId { get; set; }
public int Number1 { get; set; }
public int Number2 { get; set; }
public int Number3 { get; set; }
public int Number4 { get; set; }
public int Number5 { get; set; }
public string SortOrder { get; set; }
}
剃刀:
@{
ViewBag.Title = "csi media web test";
}
<div class="jumbotron">
<h1>csi media web test</h1>
<p class="lead">Liane Stevenson</p>
</div>
<div class="row">
<div class="col-md-12">
<div class="panel panel-info">
<div class="panel-heading"><i class="glyphicon glyphicon-arrow-right"></i> Enter Your Four Numbers</div>
<div class="panel-body">
<form class="form-inline">
<div class="col-md-9">
<div class="form-group">
<label class="sr-only" for="number1">1st Number</label>
<input type="number" class="form-control" id="number1" name="Number1" placeholder="#1">
</div>
<div class="form-group">
<label class="sr-only" for="number2">2nd Number</label>
<input type="number" class="form-control" id="number2" name="Number2" placeholder="#2">
</div>
<div class="form-group">
<label class="sr-only" for="number3">3rd Number</label>
<input type="number" class="form-control" id="number3" name="Number3" placeholder="#3">
</div>
<div class="form-group">
<label class="sr-only" for="number4">4th Number</label>
<input type="number" class="form-control" id="number4" name="Number4" placeholder="#4">
</div>
</div>
<div class="col-md-3 text-right">
<a class="btn btn-default" href="@Url.Action("SortDesc", "Home")"><i class="glyphicon glyphicon-arrow-down"></i> Sort Desc</a>
<a class="btn btn-default" href="@Url.Action("SortAsc", "Home")"><i class="glyphicon glyphicon-arrow-down"></i> Sort Asc</a>
</div>
</form>
<p>
@if (Model != null)
{
foreach (int number in Model.Numbers)
{
<span class="label label-info">@number</span>
}
}
</p>
</div>
</div>
</div>
</div>
家庭控制器:
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult SortDesc([Bind(Include = "Number1, Number2, Number3, Number4")] NumberSetList model)
{
if (!ModelState.IsValid)
{
return View();
}
else
{
NumberSetList list = new NumberSetList();
List<int> numbers = new List<int>();
numbers.Add(model.Number1);
numbers.Add(model.Number2);
numbers.Add(model.Number3);
numbers.Add(model.Number4);
numbers.OrderByDescending(i => i);
list.SortOrder = "Desc";
return View(list);
}
}
答案 0 :(得分:2)
试试这个
public class NumberSetList {
public int NumberSetListId { get; set; }
public List<NumberSetItem> Numbers { get; set; }
public string SortOrder { get; set; }
}
public class NumberSetItem {
public int Value { get; set; }
}
,使用for
循环为数字创建输入
@using (Html.BeginForm()) {
<div class="col-md-9">
<div class="form-group">
@if (Model != null && Model.Numbers != null) {
@for (int i = 0; i < Model.Numbers.Count; i++) {
<div class="form-group">
@Html.LabelFor(m => m.Numbers[i].Value, "Number " + (i+1).ToString())
@Html.TextBoxFor(m => m.Numbers[i].Value, new { @class = "form-control" })
</div>
}
<div class="col-md-3 text-right">
<input id="SortOrder" value="Desc" type="button" class="btn btn-default"><i class="glyphicon glyphicon-arrow-down"></i> Sort Desc</input>
<input id="SortOrder" value="Asc" type="button" class="btn btn-default"><i class="glyphicon glyphicon-arrow-down"></i> Sort Asc</input>
</div>
}
</div>
</div>
}
在视图中
[HttpPost]
public ActionResult Index(NumberSetList model) {
if (!ModelState.IsValid) {
return View();
} else {
var numbers = model.SortOrder == "Desc" ?
model.Numbers.OrderByDescending(n => n.Value) :
model.Numbers.OrderBy(n => n.Value);
model.Numbers = numbers.ToList();
return View(model);
}
}
答案 1 :(得分:1)
您可以使用各种技术解决问题。
将视图名称传递给View
方法,如下所示:
return View("Index", list);
View
方法有很多重载,你可以看到它们here。熟悉您所面临的场景的重载是很好的。
如果您的视图与操作不在同一文件夹中,则需要传递视图的路径,而不仅仅是名称。
另一种方法是只有一个动作,你可以调用这个索引,但是这个将把模型作为参数。您的模型可以包含数字和枚举:Desc,Asc。在您的操作方法中,您可以分析模型,并执行所需的任何操作。
模型的方法是我喜欢的方法。
修改强>
我刚刚注意到你在视图中有这行代码:
<a class="btn btn-default" href="@Url.Action("SortDesc", "Home")"><i class="glyphicon glyphicon-arrow-down"></i> Sort Desc</a>
这将触发HTTP GET,因此它不会触及您的SortDsc
操作方法。从您的操作方法中删除POST或使用post技术(Form)使用POST命中您的操作方法。
编辑2
老实说,你所做的事情远比你做的简单得多。首先,当你使用Url.Actio
时,你没有向你的动作方法传递任何内容,那么它将如何知道这些数字呢?我通常会使用AJAX调用我的action方法并获取结果并显示结果。
如果没有AJAX,您需要使用一个表单进行升序,一个表单用于降序。或者您可以使用单选按钮,以便用户可以先选择订单。
以下是使用单选按钮执行此操作的一些代码。
动作:
[HttpPost]
public ActionResult SortDesc([Bind( Include = "Number1, Number2, Number3, Number4, SortOrder" )] NumberList model) {
if( !ModelState.IsValid ) {
return View("Index");
}
model.Numbers = new List<int>();
model.Numbers.Add( model.Number1 );
model.Numbers.Add( model.Number2 );
model.Numbers.Add( model.Number3 );
model.Numbers.Add( model.Number4 );
model.Numbers.Add( model.Number5 );
if (model.SortOrder == "Desc" ) {
model.Numbers = model.Numbers.OrderByDescending( i => i ).ToList();
}
else {
model.Numbers = model.Numbers.OrderBy( i => i ).ToList();
}
return View("Index", model);
}
型号:
public class NumberList {
public int Number1 { get; set; }
public int Number2 { get; set; }
public int Number3 { get; set; }
public int Number4 { get; set; }
public int Number5 { get; set; }
public int NumberListId { get; set; }
public List<int> Numbers { get; set; }
public string SortOrder { get; set; }
}
查看:
@using( Html.BeginForm( "SortDesc", "Home", FormMethod.Post ) ) {
<div class="row">
<div class="col-md-12">
<div class="panel panel-info">
<div class="panel-heading"><i class="glyphicon glyphicon-arrow-right"></i> Enter Your Four Numbers</div>
<div class="panel-body">
<form class="form-inline">
<div class="col-md-9">
<div class="form-group">
<label class="sr-only" for="number1">1st Number</label>
<input type="number" class="form-control" id="number1" name="Number1" placeholder="#1">
</div>
<div class="form-group">
<label class="sr-only" for="number2">2nd Number</label>
<input type="number" class="form-control" id="number2" name="Number2" placeholder="#2">
</div>
<div class="form-group">
<label class="sr-only" for="number3">3rd Number</label>
<input type="number" class="form-control" id="number3" name="Number3" placeholder="#3">
</div>
<div class="form-group">
<label class="sr-only" for="number4">4th Number</label>
<input type="number" class="form-control" id="number4" name="Number4" placeholder="#4">
</div>
<div class="form-group">
<input type="radio" class="form-control" id="number4" name="SortOrder" value="Desc">
</div>
<div class="form-group">
<input type="radio" class="form-control" id="number4" name="SortOrder" value="Asc">
</div>
</div>
<div class="col-md-3 text-right">
<button class="btn btn-default"><i class="glyphicon glyphicon-arrow-down"></i> Sort </button>
@*<a class="btn btn-default"><i class="glyphicon glyphicon-arrow-down"></i> Sort Asc</a>*@
</div>
</form>
<p>
@if( Model != null ) {
foreach( int number in Model.Numbers ) {
<span class="label label-info">@number</span>
}
}
</p>
</div>
</div>
</div>
</div>
}