在下面的代码中,我在min = 0, max = 99
方法中重复了Html.EditorFor()
个值。
使其更干燥的最佳方法是什么?
<div id="Team1PlayerScores">
@for (int i = 0; i < Model.Team1.Players.Count; i++)
{
@Html.DisplayFor(m => Model.Team1.Players[i].Name)
@Html.EditorFor(m => Model.Team1.Players[i].GoalsForCurrentGame, new { htmlAttributes = new { min = 0, max = 99, onchange = "updateTeamScore('Team1PlayerScores', 'Team1_GoalsForCurrentGame')" } })
}
</div>
<div id="Team2PlayerScores">
@for (int i = 0; i < Model.Team2.Players.Count; i++)
{
@Html.DisplayFor(m => Model.Team2.Players[i].Name)
@Html.EditorFor(m => Model.Team2.Players[i].GoalsForCurrentGame, new { htmlAttributes = new { min = 0, max = 99, onchange = "updateTeamScore('Team2PlayerScores', 'Team2_GoalsForCurrentGame')" } })
}
</div>
答案 0 :(得分:0)
如果您要删除代码中“魔术数字”的重复项,而不是生成的HTML,则可以用模型中的值替换数字:
\b(?<![.\d])([1-9][0-9]{1,2}\.[1-9]|[1-9][0-9]{1,2})(?=inch)
对视图进行更新以利用它:
public class IndexModel
{
public Team Team1 { get; set; }
public Team Team2 { get; set; }
public int MinimumGoals => 0;
public int MaximumGoals => 99;
}
我已将值作为只读属性放入,但是如何将其实现到模型中由您决定。