\Calendar control is one of the most import part in a web or
windows application. If we are talking about web application on that case Asp.net and MVC application comes in picture. In this article I am going to show you how
you can use jQuery datetime picker control in your MVC application an how you retrieve
the sleeted date at controller end.
Some of my asp.net and mvs applications are as follows :
Syntax
For Switch In C#.Net and VB.Net , Random
Row With LINQ To SQL Using C#.Net | Randomly Select Records Using Linq to SQL
Using C# , Merge
two datatables in C#.Net and VB.Net | How to merge multiple datatables using
C#.Net , How
to implement windows authentication in asp.net mvc .
Some of eBooks which you like to download for MVC :
Beginning
jQuery 2 for ASP.NET Developers - Free download ebooks , Free
Download eBook - Exam Ref 70-486: Developing ASP.NET MVC 4 Web Applications |
MVC 4 Microsoft certification eBook , Getting
Started with Twitter Flight - Free Download eBook.
So for this article first you create a new MVC application.
In this add a controller and create view for controller action.
Now add a post method in your controller.
So here is the code of controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace ProjectDemoweb.Controllers
{
public class HomeController
: Controller
{
//
// GET:
/Home/
[HttpGet]
public
ActionResult Index()
{
return
View();
}
[HttpPost]
public
ActionResult Index(string Datepicker)
{
ViewBag.DateValue = Datepicker;
return
View();
}
}
}
|
Now add the below code in your controller view .
@{
ViewBag.Title = "Use
of jQuery Datepicker In an MVC Application";
}
<link rel="stylesheet"
href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
$(function
() {
$("#txtdatepicker").datepicker();
});
</script>
<h2>jQuery
Datepicker</h2>
@using
(Html.BeginForm("Index", "Home"))
{
<p>
@Html.TextBox("Datepicker","", new
{ @id = "txtdatepicker",@style="width:150px;" })
<br />
Your selected date :@ViewBag.DateValue
<br /><input type="submit" value="Submit" />
</p>
}
|
Here in above view codes below section have been used for displaying
the calendar control:
<link rel="stylesheet"
href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
$(function
() {
$("#txtdatepicker").datepicker();
});
</script>
|
Now run the application. Select the date and click submit
button. In your controller you will get selected data as shown below. Here the most important thing you have to
take care that TextBox Control name and the parameter passed in your controller
post action is same. If TextBox name and parameter name will differ on that
case you will not get the selected value.
Final output:
DOWNLOAD
0 comments:
Please let me know your view