ViewModel returns 默认值为0
ViewModel returns a default value of 0
我正在做一个房地产管理信息系统。我有这个 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 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; }
}
我在这个控制器中使用它:
public ActionResult Create()
{
UnitViewModel unitVM = new UnitViewModel();
unitVM.HouseModels = db.HouseModels.ToList();
unitVM.Sites = db.Sites.ToList();
return View(unitVM);
}
但是,当我 运行 应用程序时,它给了我这个输出。
有没有办法删除那些 0 默认值?感谢您的帮助。
将块 属性 类型从 Int
更改为 Nullable int
public class UnitViewModel
{
public IEnumerable<HouseModel> HouseModels { get; set; }
public int SelectedModelID { get; set; }
public int? Block { get; set; }
// Other properties goes here
}
由于 Block 是一个可为 null 的 int,因此在访问它并调用其上的任何方法之前进行 null 检查总是一个好主意。
[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
}
我正在做一个房地产管理信息系统。我有这个 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 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; }
}
我在这个控制器中使用它:
public ActionResult Create()
{
UnitViewModel unitVM = new UnitViewModel();
unitVM.HouseModels = db.HouseModels.ToList();
unitVM.Sites = db.Sites.ToList();
return View(unitVM);
}
但是,当我 运行 应用程序时,它给了我这个输出。
有没有办法删除那些 0 默认值?感谢您的帮助。
将块 属性 类型从 Int
更改为 Nullable int
public class UnitViewModel
{
public IEnumerable<HouseModel> HouseModels { get; set; }
public int SelectedModelID { get; set; }
public int? Block { get; set; }
// Other properties goes here
}
由于 Block 是一个可为 null 的 int,因此在访问它并调用其上的任何方法之前进行 null 检查总是一个好主意。
[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
}