如何在mvc中设置默认值 [英] how to set default value in mvc
本文介绍了如何在mvc中设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
这里我想在实体模型中设置默认值。
描述:
这里我在控制器中设置默认值但现在我想在实体模型中设置默认值。
这是实体模型中的代码: -
hi to all,
Here i want to set default value in entities model.
DESCRIPTION:
here i set default values in controller but now i want to set default in entities model.
Here is my code in entities model:-
[StringLength(1)]
[Column(TypeName = "char")]
public string IsActive { get; set; }
在控制器中: -
In controller:-
public ActionResult _CreatePartialView() student Student)
{
if (ModelState.IsValid == true)
{
branch.IsActive = "Y";
db.Students.Add(Student);
db.SaveChanges();
}
以上代码我已经使用但现在我想设置实体模型中的默认值(即)获取集方法
推荐答案
您可以在实体的构造函数中设置默认值,例如
You could set your default values in the constructor for the entity, e.g.
public class MyEntity
{
public MyEntity()
{
IsActive = "Maybe";
}
[StringLength(1)]
[Column(TypeName = "char")]
public string IsActive { get; set; }
}
这篇关于如何在mvc中设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文