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.
Some of my previous articles are as follows: Bind
DropDownList In MVC WebGrid and Retrive Value Using Asp.net MVC, C#.Net, Add
Textbox In WebGrid and Access TextBox Value In Controller In Asp.net MVC Using
C#.Net, FileUpload
Control Inside WebGrid To Upload File In Asp.net MVC Using C#.Net, Auto
Increment Row Value In Asp.net MVC WebGrid Using C#.Net, Frozen
Rows and Columns in Asp.Net Mvc Webgrid Using jQuery Like Excel Sheet, Code
to Dynamically Bind Image To Asp.net MVC WebGrid.
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.
0 comments:
Please let me know your view