Monday, 8 September 2014

Bind jQuery DatePicker Calendar In MVC WebGrid and Retrive Value Using Asp.net MVC, C#.Net

9/08/2014 - By Pranav Singh 0

This article will show you how you can use jQuery DatePicker Calendar inside your asp.net mvc webgrid control and how you can retrieve the selected date at your controller end in your asp.net mvc application using c#.net.


So for this article first we will create a new asp.net application and add a model class file into model folder. Now add the below code into your into your model class file.

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

namespace MvcApplication6.Models
{
    public class StudentModel
    {
        public List<Student> StudentList { get; set; }
    }
    public class Student
    {
        public string Name { get; set; }
        public string ClassOfStudent { get; set; }
        public string DOB { get; set; }
        public string StudentSection { get; set; }
        public string Address { get; set; }
    }
}

Now we will add the controller file into our application and after adding controller class we will add the below code into you controller class file.

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

namespace MvcApplication6.Models
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            StudentModel _objstudentmodel = new StudentModel();
            _objstudentmodel = StudentRecord();
            return View(_objstudentmodel);
        }
        [HttpPost]
        public ActionResult Index(StudentModel _objstudentmodel)
        {
            return View(_objstudentmodel);
        }
        ///


        /// This function will return data for webgrid
        ///
        public StudentModel StudentRecord()
        {
            StudentModel _objstudentmodel = new StudentModel();
            _objstudentmodel.StudentList = new List<Student>();
            _objstudentmodel.StudentList.Add(new Student { Name = "Name1", ClassOfStudent ="12th", Address = "Address1" });
            _objstudentmodel.StudentList.Add(new Student { Name = "Name2", ClassOfStudent ="5th", Address = "Address2" });
            _objstudentmodel.StudentList.Add(new Student { Name = "Name3", ClassOfStudent ="10th", Address = "Address3" });
            _objstudentmodel.StudentList.Add(new Student { Name = "Name4", ClassOfStudent ="1st", Address = "Address4" });

            return _objstudentmodel;
        }
    }
}
        
Here in above code I have added some value in student collection for making data to display into webgrid.  After that I have passed that collection to the view for displaying on page.

After that I have added post method to handle the submit button event to retrieve the data of selected date at controller end.

Now we will create view and add the below code into your view.

@model  MvcApplication6.Models.StudentModel
@{
    ViewBag.Title = "Bind jQuery DatePicker Calendar In MVC WebGrid and Retrive Value Using Asp.net MVC, C#.Net";
}
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.0/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.11.0/jquery-ui.js"></script>
<style>
    body
    {
        font-size: 70%;
    }
</style>
<script type="text/javascript" language="javascript">
    $(function () {
        $(".calendarcss").datepicker({
            showOn: "button",
            buttonImage: "images/calendar-icon.png",
            buttonImageOnly: true,
            buttonText: "Select date"
        });
    });
</script>
<style type="text/css">
    table
    {
        font-family: verdana,arial,sans-serif;
        font-size: 11px;
        color: #333333;
        border-width: 1px;
        border-color: #666666;
        border-collapse: collapse;
    }
    table th
    {
        border-width: 1px;
        padding: 8px;
        border-style: solid;
        border-color: #666666;
        background-color: #dedede;
    }
    table td
    {
        border-width: 1px;
        padding: 8px;
        border-style: solid;
        border-color: #666666;
        background-color: #ffffff;
    }
    input
    {
        width: 60px;
    }
</style>
@using (Html.BeginForm("Index", "Home"))
{

    var grid = new WebGrid(Model.StudentList, canSort: false, canPage: false);
    int rowNum = 0;
    <div>
        @grid.GetHtml(columns:
            grid.Columns
            (
                grid.Column("RowNumber", format: item => rowNum = rowNum + 1),
                grid.Column("Name", format: (item) => Html.TextBox("StudentList[" + (rowNum - 1).ToString() + "].Name", (object)item.Name)),
                grid.Column("Class", format: (item) => Html.TextBox("StudentList[" + (rowNum - 1).ToString() + "].ClassOfStudent", (object)item.ClassOfStudent)),
                  grid.Column("DOB", format: (item) => Html.TextBox("StudentList[" + (rowNum - 1).ToString() + "].DOB", (object)item.DOB, new { @class = "calendarcss",@style="width:70px;" })),
                grid.Column("Address", format: (item) => Html.TextBox("StudentList[" + (rowNum - 1).ToString() + "].Address", (object)item.Address))
            ), mode: WebGridPagerModes.Numeric)
    </div>
    <input type="submit" value="Submit" />   
   
}

In above code you just check the control name this I have made dynamic because in webgrid data in the collection form and when post back take place at that time at controller end to get the complete collection.

Now we have done just run the application to check the output.


Now select data from calendar.


In above code I have selected date only in first three columns. This I have done to demonstrate that for values. Now click on submit button. You break point will hit and you will get output as shown below.



In above we are getting the date which we have selected.




And in above we are getting null because we have not selected any date value.

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