February 27 2014

ASP.net how to set model default value

Tagged Under : ,

asp.net
Because I want to show default value in my create page. and in the create page code was used Html.TextBox and model to generate the input box.

Since it was used model to generate the code and we also can use model to set the default value as well.

Below example code are showing how set the default value in model:
public class Category 
{
  public int ID { get; set; }
  public string Name { get; set; }
  public DateTime CreateDate { get; set; }
}
To set the default value, we need define another public. and the name mush same as the model.
public class Category 
{
  public int ID { get; set; }
  public string Name { get; set; }
  public DateTime CreateDate { get; set; }

  public Category()
  {
    Name = "New Category Name";
  }
}
After that, try refresh the view page and you will able to see the default value display inside the text box.

Make a Comment

You must be logged in to post a comment.