C# ViewModel返回默认值0
我正在开发一个房地产管理信息系统。我有这个模型:C# ViewModel返回默认值0,c#,asp.net-mvc,viewmodel,C#,Asp.net Mvc,Viewmodel,我正在开发一个房地产管理信息系统。我有这个模型: public class UnitViewModel { public IEnumerable<HouseModel> HouseModels { get; set; } public int SelectedModelID { get; set; } public int Block { get; set; } public int FromLot { get; set; } public
public class UnitViewModel
{
public IEnumerable<HouseModel> HouseModels { get; set; }
public int SelectedModelID { get; set; }
public int Block { get; set; }
public int FromLot { get; set; }
public int ToLot { get; set; }
public double LotArea { get; set; }
public double FloorArea { get; set; }
public IEnumerable<Site> Sites { get; set; }
public int SelectedSiteID { get; set; }
public double Price { get; set; }
}
但是,当我运行应用程序时,它会给我这个输出
有没有办法删除这0个默认值?感谢您的帮助。将块属性类型从Int更改为可为null的Int 您可以使用此可为null的属性上的数据批注进行验证
public class UnitViewModel
{
[Required]
public int? Block { get; set; }
// Other properties goes here
}
将块特性类型从Int更改为可为null的Int 您可以使用此可为null的属性上的数据批注进行验证
public class UnitViewModel
{
[Required]
public int? Block { get; set; }
// Other properties goes here
}
如果初始值为null,则可以使属性为null,那么文本框中将不显示任何内容。如果需要[Required]属性,您可能还希望添加该属性。这听起来可能有矛盾,但是我可以在可空类型中添加一个Required属性吗?谢谢当然可以-[RequiredErrorMessage=请输入块]公共int?Block{get;set;}如果初始值为null,则可以使属性为null,那么文本框中将不显示任何内容。如果需要[Required]属性,您可能还希望添加该属性。这听起来可能有矛盾,但是我可以在可空类型中添加一个Required属性吗?谢谢当然可以-[RequiredErrorMessage=请输入块]公共int?Block{get;set;}谢谢你的帮助。但是,我可以为此类属性添加数据注释吗?”因为我有一个限制条件,没有区块和地块就无法创建单位@Shyju@freedomn-m、 什么是犯规?Google没有帮助meYes@Cyval您可以在该属性上拥有一个必需的属性来帮助您进行验证。非常感谢,并祝您拥有70k的声誉:欢迎光临:你们都很棒;谢谢你的邀请。但是,我可以为此类属性添加数据注释吗?”因为我有一个限制条件,没有区块和地块就无法创建单位@Shyju@freedomn-m、 什么是犯规?Google没有帮助meYes@Cyval您可以在该属性上拥有一个必需的属性来帮助您进行验证。非常感谢,并祝您拥有70k的声誉:欢迎光临:你们都很棒;
[Httppost]
public ActionResult Create(UnitViewModel model)
{
if(model.Block!=null)
{
int blockValue= model.Block.Value;
// do something now
}
// to do : Do something and return something
}
public class UnitViewModel
{
[Required]
public int? Block { get; set; }
// Other properties goes here
}