Monday, 19 May 2014

jQuery DateTime Picker Calender In ASP.NET MVC | How To Access jQuery DateTime Picker Calender Value in MVC Controller

5/19/2014 - By Pranav Singh 0

\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.



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

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top