Today I will show you how you can display a Calander control in TextBoxFor control in asp.net core 8 MVC application using C#.Net and how we can retrieve selected from view to controller.
Now fort this article we will first create a new asp.net core mvc application and a model class file. In this class file add the below code.
namespace Project.Models
{
public class UserModel
{
public DateTime DateOfBirth { get; set; }
}
}
Now we will add controller and add the below code in it.
[HttpGet]
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(UserModel userModel)
{
ViewBag.SelectedDate = userModel.DateOfBirth;
return View();
}
In above code I have added two IActionResult methods of Type HttpGet and HttpPost. Here method with HttpGet attribute will act as page load and method with HttpPost will execute on form post. In HttpPost method i have assigned the model class as parameter. and on Submit of the button control we will receive the selected value. Now we will create a view and add the below code in it.
@model UserModel
@{
ViewData["Title"] = "Home Page";
}
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @Id = "formdata", @enctype = "multipart/form-data" }))
{
<div class="text-center">
<b>Select Date Time:</b>
@Html.TextBoxFor(m => m.DateOfBirth, new { @class = "form-control", @type = "date" })
<br />
<input type="submit" value="Submit" />
<br /> <br />
<b>Your Selected Date: </b>@ViewBag.SelectedDate
</div>
}