DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • GDPR Compliance With .NET: Securing Data the Right Way
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Zero to AI Hero, Part 3: Unleashing the Power of Agents in Semantic Kernel
  • Developing Minimal APIs Quickly With Open Source ASP.NET Core

Trending

  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 1
  • How Can Developers Drive Innovation by Combining IoT and AI?
  • How to Build Real-Time BI Systems: Architecture, Code, and Best Practices
  • Customer 360: Fraud Detection in Fintech With PySpark and ML
  1. DZone
  2. Coding
  3. Frameworks
  4. Setting Default Value for @Html.EditorFor in ASP.NET MVC

Setting Default Value for @Html.EditorFor in ASP.NET MVC

In this blog post I am going to Explain How we create Default values for model Entities.

By 
Jalpesh Vadgama user avatar
Jalpesh Vadgama
·
Aug. 21, 11 · Tutorial
Likes (0)
Comment
Save
Tweet
Share
72.3K Views

Join the DZone community and get the full member experience.

Join For Free

Yesterday one of my friend asked me to set default value for @HTML.EditorFor. So I decided to write this blog post. In this blog post I am going to Explain How we create Default values for model Entities. So Let’s start this via taking a simple example Model class called User. In User Model class I have created two properties UserName and UserJoinedDate. Following is a code for that.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CodeSimplified.Models
{
 public class User
  {
      private DateTime _userJoinDate = DateTime.Now;

      public DateTime UserJoinDate
      {
        get
          {
              return _userJoinDate;
          }
          set
          {
              _userJoinDate = value;
          }
      }

      public string UserName
      { get; set; }
  }
}

As you can see in above class username is default property while for UserJoinedDate I have used old method of declaring properties. Where I have assigned a private variable with System DateTime.

Now let’s Create a Action Result User in Home Controller like following where I am returning a new User View like following.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CodeSimplified.Controllers
{
  public class HomeController : Controller
  {
      public ActionResult Index()
      {
          ViewBag.Message = "Welcome to ASP.NET MVC!";

          return View();
      }

      public ActionResult About()
      {
          return View();
      }

      public ActionResult User()
      { return View(new CodeSimplified.Models.User()); }
  }
}

Now Let’s Create User View from the action right click Add view like following. Here I have created Strongly Typed view like following.

User

Once you click Add It will add a new View like following.

@model CodeSimplified.Models.User

@{
  ViewBag.Title = "User";
}

<h2>User</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
  @Html.ValidationSummary(true)
  <fieldset>
      <legend>User</legend>

      <div class="editor-label">
          @Html.LabelFor(model => model.UserJoinDate)
      </div>
      <div class="editor-field">
          @Html.EditorFor(model => model.UserJoinDate)
          @Html.ValidationMessageFor(model => model.UserJoinDate)
      </div>

      <div class="editor-label">
          @Html.LabelFor(model => model.UserName)
      </div>
      <div class="editor-field">
          @Html.te
          @Html.EditorFor(model => model.UserName)
          @Html.ValidationMessageFor(model => model.UserName)
      </div>

      <p>
          <input type="submit" value="Create" />
      </p>
  </fieldset>
}

<div>
  @Html.ActionLink("Back to List", "Index")
</div>

Now everything is ready so Let's run that in browser. Following is the output as expected.

Browser

So that’s it. It’s very easy. Hope you liked it. Stay tuned for more.. Till then happy programming.

 

ASP.NET MVC ASP.NET

Published at DZone with permission of Jalpesh Vadgama, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • GDPR Compliance With .NET: Securing Data the Right Way
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Zero to AI Hero, Part 3: Unleashing the Power of Agents in Semantic Kernel
  • Developing Minimal APIs Quickly With Open Source ASP.NET Core

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: