我有一个名为 protected void btnSave_Click(object sender, EventArgs e)
{
player _player = new player();
Guid id = new Guid(Request.QueryString["id"].ToString());
Guid teamId = new Guid(Request.QueryString["teamId"].ToString());
if (teamId != Guid.Empty)
_player.teamId = teamId;
if (id == Guid.Empty)
{
_player.Name = txtFullName.Text;
_player.address = txtAddress.Text;
_player.player_id = _dal.GetNextPlayerId(16, 45);
_player.gender = dlGenders.SelectedValue.ToString();
_dal.SoccerEntities.AddToplayers(_player);
_dal.SoccerEntities.SaveChanges();
Response.Redirect("default.aspx");
}
else
{
Guid _playerId = new Guid(Request.QueryString["id"].ToString());
_player = _dal.GetPlayerBYID(_playerId);
if (_player.player_id == "")
_player.player_id = _dal.GetNextPlayerId(16, 45);
_player.gender = dlGenders.SelectedValue.ToString();
_player.Name = txtFullName.Text;
_player.address = txtAddress.Text;
_player.player_id = _dal.GetNextPlayerId(16, 45);
_player.gender = dlGenders.SelectedValue.ToString();
if (teamId != Guid.Empty)
_player.teamId = teamId;
_dal.SoccerEntities.SaveChanges();
Response.Redirect("default.aspx");
}
的状态键,其值可以在0到100之间。有没有办法可以对这些值进行约束?
知道我做了一个新方法(但它看起来过度工程):
```
zoom
是否有更优雅的解决方案将约束应用于状态键?
答案 0 :(得分:1)
最好的方法取决于约束的复杂性。
如果约束非常简单,你可以内联,就像这样:
this.setState ( { zoom : min(100, max(value, 0)) } );
更具可读性:
var validatedZoom = min(100, max(value, 0));
this.setState( { zoom : validatedZoom } );
如果验证更复杂(例如为用户生成消息)或验证需要可重用,则单独的验证功能可能是最好的方法。在简单的例子中:
function validatedZoom(value) {
return min(100, max(value, 0));
}
// elsewhere
this.setState( { zoom : validatedZoom(value) } );
或@janakastevens提供的答案
答案 1 :(得分:0)
它可能看起来像这样。
handleOnChange(event){
var newValue = this.checkZoomValue(event.target.value);
this.setState({zoom:newValue});
}
checkZoomValue(value) {
var outValue = min(value, 100)
outValue = max(value, 0);
return outValue;
}