在我的MVC足球应用程序的一些地方,我使用同一列使用订单,但只是在一个地方它的工作错误,我不知道为什么。
这是我的圆形模型:
public class Round : IValidatableObject
{
public int RoundID { get; set; }
public int RoundNumber { get; set; }
public DateTime RoundStart { get; set; }
public DateTime RoundEnd { get; set; }
public virtual ICollection<Match> Matches { get; set; }
}
我的匹配模型:
public class Match
{
public int MatchId { get; set; }
public string HomeClubName { get; set; }
public string AwayClubName { get; set; }
public virtual Club HomeClub { get; set; }
public virtual Club AwayClub { get; set; }
public int? Club1Goals { get; set; }
public int? Club2Goals { get; set; }
[Required]
public int RoundNumber { get; set; }
[ForeignKey("RoundNumber")]
[InverseProperty("Matches")]
public virtual Round Round { get; set; }
}
我按来自Round类的RoundNumber列进行排序。它适用于我的整个项目:
return View(db.Round.ToList().OrderBy(s => s.RoundNumber));
它就像一个魅力。但是在一个地方它的工作是错误的。它位于匹配列表中(每个匹配都有一个整数)。这是以下代码:
public ActionResult Index()
{
if (User.Identity.IsAuthenticated)
{
if (isAdminUser())
{
return View(db.Round.ToList().OrderBy(s => s.RoundNumber));
}
else return RedirectToAction("Index", "Home");
}
else
{
ViewBag.Name = "Not Logged IN";
}
return View(db.Round.ToList().OrderBy(s => s.RoundNumber));
}
我觉得可能会按RoundI
而不是RoundNumber
进行排序,因为我按以下顺序添加了回合:
20,21,22,23,24,25,19,18,1,3,33,32,11
测试我的理论,以及他们在这个特定视图中出现的顺序(我希望按照他们的数字对匹配进行排序)。这是创建新匹配的代码:
[Authorize]
public ActionResult Create()
{
if (isAdminUser())
{
ViewBag.RoundNumber = new SelectList(db.Round.OrderBy(s => s.RoundNumber), "RoundID", "RoundNumber");
ViewBag.AwayClubName = new SelectList(db.Clubs, "ClubName", "ClubName");
ViewBag.HomeClubName = new SelectList(db.Clubs, "ClubName", "ClubName");
return View();
}
else return RedirectToAction("Index", "Matches1");
}
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize]
public ActionResult Create([Bind(Include = "MatchId,HomeClubName,AwayClubName,Club1Goals,Club2Goals,RoundNumber")] Models.Match match)
{
if (isAdminUser())
{
if (ModelState.IsValid)
{
if (match.HomeClubName.Equals(match.AwayClubName))
{
ViewBag.RoundNumber = new SelectList(db.Round.OrderBy(s => s.RoundNumber), "RoundID", "RoundNumber", match.RoundNumber);
ViewBag.AwayClubName = new SelectList(db.Clubs, "ClubName", "ClubName", match.AwayClubName);
ViewBag.HomeClubName = new SelectList(db.Clubs, "ClubName", "ClubName", match.HomeClubName);
return View(match);
}
db.Match.Add(match);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.RoundNumber = new SelectList(db.Round.OrderBy(s => s.RoundNumber), "RoundID", "RoundNumber", match.RoundNumber);
ViewBag.AwayClubName = new SelectList(db.Clubs, "ClubName", "ClubName", match.AwayClubName);
ViewBag.HomeClubName = new SelectList(db.Clubs, "ClubName", "ClubName", match.HomeClubName);
return View(match);
}
else return RedirectToAction("Index", "Matches1");
}
它的观点:
@model ProjSty3Exp.Models.Match
@{
ViewBag.Title = "Add";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="main_bg">
<div class="wrap">
<div class="sap_tabs">
<div id="horizontalTab" style="display: block; width: 100%; margin: 0px;">
<div class="tekst" style="font-size: 1em; color: #d9d9d9; display: block; line-height:1.5em; margin-left: 5%">
<br />
@Html.Raw(ViewBag.Error)
<br />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.HomeClubName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("HomeClubName", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.HomeClubName, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group">
@Html.LabelFor(model => model.AwayClubName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("AwayClubName", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.AwayClubName, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group">
Numer kolejki:
<div class="col-md-10">
@Html.DropDownList("RoundNumber", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.RoundNumber, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Dodaj mecz" class="btn btn-default" />
</div>
</div>
<br />
<div>
@Html.ActionLink("Powrót do panelu administratora", "Index", "Admin")
</div>
</div>
<br />
</div>
</div>
</div>
</div>
}
是否正在创建一个打破它的新匹配? RoundNumber为int
而不是string
,因此它不是字母数字排序问题。它在单独显示轮次列表时或在创建匹配时在下拉列表中进行排序。
为什么它按RoundId(可能)排序而不是RoundNumber呢? 如果有人知道发生了什么或如何解决它,我会非常感激。