How your action method looks like ? Can you post some code ? I don't see you named your datepicker input, how are you getting the value of date input ?
However, I thought I would use a specific BeginForm just for the datePicker. I would like for the action to be called by the on change instead of by clicking a button
[HttpPost]
public ActionResult ClassAttendance(InstructorIndexData viewModel, int id, FormCollection frmcol, int rows, String sTeacher, DateTime? date=null)
{
var dateValue = frmCol["myDate"];
// dateValue should be the Date selected
}
Marked as answer by AlexanderBlade on Nov 16, 2012 10:54 PM
AlexanderBla...
Member
325 Points
309 Posts
How can I use the jQuery onChange() event to send the selected date from my datePicker to my Cont...
Nov 16, 2012 06:54 PM|LINK
How can I use the jQuery onChange() event to send the selected date from my datePicker to my Controller?
Here’s what I’ve been trying but in my controller the ‘date’ parameter is always null:
The script:
<script type="text/javascript">
$(function () {
$(".datepicker").datepicker({dateFormat: 'yy.mm.dd'})
});
</script>
Here I’ve created a form just for the datePicker input field. Although I have other BeginForms in the view:
@using (Html.BeginForm("Action", "Controller", new { @id = "formName", @Name = "formName" }))
{
<p>Date: <input type="text" class="datepicker" onchange="$('#formName').submit();" /></p>
}
The controller:
{
if (date != null)
{
//do something
}
}
In the controller, date is always null. Am I naming my BeginForm wrong?
_Manvel_
Contributor
4242 Points
923 Posts
Re: How can I use the jQuery onChange() event to send the selected date from my datePicker to my ...
Nov 16, 2012 07:10 PM|LINK
How your action method looks like ? Can you post some code ? I don't see you named your datepicker input, how are you getting the value of date input ?
AlexanderBla...
Member
325 Points
309 Posts
Re: How can I use the jQuery onChange() event to send the selected date from my datePicker to my ...
Nov 16, 2012 08:00 PM|LINK
[HttpPost]
public ActionResult ClassAttendance(InstructorIndexData viewModel, int id, FormCollection frmcol, int rows, String sTeacher, DateTime? date=null)
{
if (date != null)
{
}
string[] AllInstructors = frmcol["item.Instructor"].Split(',');
string[] AllFstMNames = frmcol["item.Student.FirstMidName"].Split(',');
//more formcollection fields here
}
Currently I’m using this BeginForm to call the action:
@using (Html.BeginForm("ClassAttendance", "Attendance", new { rows = Model.Enrollments.Count(), currentDate = day, id = @ViewBag.ID, sTeacher = @ViewBag.teacherName, courseID = HttpContext.Current.Session["sCourseID"] }, FormMethod.Post))
<!— fields here……>
<Input Type ="submit" Value="Submit Attendance"/>
{
However, I thought I would use a specific BeginForm just for the datePicker. I would like for the action to be called by the on change instead of by clicking a button
_Manvel_
Contributor
4242 Points
923 Posts
Re: How can I use the jQuery onChange() event to send the selected date from my datePicker to my ...
Nov 16, 2012 09:15 PM|LINK
Try to give a name to your input control
<p>Date: <input name="myDate" type="text" class="datepicker" onchange="$('#formName').submit();" /></p>Then, read date from FormCollection
[HttpPost] public ActionResult ClassAttendance(InstructorIndexData viewModel, int id, FormCollection frmcol, int rows, String sTeacher, DateTime? date=null) { var dateValue = frmCol["myDate"]; // dateValue should be the Date selected }AlexanderBla...
Member
325 Points
309 Posts
Re: How can I use the jQuery onChange() event to send the selected date from my datePicker to my ...
Nov 16, 2012 10:33 PM|LINK
So I tried adding the name attribute like this..
<input name="myDate" type="text" class="datepicker" onchange="$('#formName').submit();" /></p>
…and in my controller I did
public ActionResult Search(Int32? id, Int32? courseID, String teacher, String course, String searchString, FormCollection frm)
{
I tried this…
string[] AllFstMNames = frm["myDate"].Split(',');
and this…
var dateValue = frm["myDate"];
string dv = dateValue.ToString();
but myDate has an empty string. Any ideas why it’s not sending the selected date along?
AlexanderBla...
Member
325 Points
309 Posts
Re: How can I use the jQuery onChange() event to send the selected date from my datePicker to my ...
Nov 16, 2012 10:54 PM|LINK
Never mind... it absolutely did work. I did something stupid earlier. Thank you so much.