i know there are a lot questions like this but i really cant get the explanations on the answers… here is my view…
<script type="text/javascript" language="javascript">
$(function () {
$(".datepicker").datepicker({ onSelect: function (dateText, inst) { },
altField: ".alternate"
});
});
</script>
@model IEnumerable<CormanReservation.Models.Reservation>
@{
ViewBag.Title = "Index";
}
<h5>
Select a date and see reservations...</h5>
<div>
<div class="datepicker">
</div>
<input name="dateInput" type="text" class="alternate" />
</div>
i want to get the value of the input text… there’s already a value in my input text because the datepicker passes its value on it… what i cant do is to pass it to my controller… here is my controller:
private CormantReservationEntities db = new CormantReservationEntities();
public ActionResult Index(string dateInput )
{
DateTime date = Convert.ToDateTime(dateInput);
var reservations = db.Reservations.Where(r=>r.Date==date).Include(r => r.Employee).Include(r => r.Room).OrderByDescending(r => r.Date);
return View(reservations.ToList());
}
i am trying to list in my home page the reservations made during the date the user selected in my calender in my home page….
I don’t see a Form tag in your View…or are you not showing the whole view? hard to tell.. but to post to your controller you should either send the value to the controller via an ajax call or post a model. In your case, your model is an
IEnumerable<CormanReservation.Models.Reservation>and your input is a date selector and doesn’t look like it is bound to your ViewModel. At what point are you posting the date back to the server? Do you have a form with submit button or do you have an ajax call that you aren’t showing?Here is an example of an Ajax request that could be called to pass in your date
Your datepicker control needs something to reference it by
Your action could then look something like this
Update
If you want to make this a standard post where you post data and return a view then you need to make changes similar to this.
Create a ViewModel
Modify your controller actions to initially load the page and then be able to post data return the View back with data
Modify your view so that you have a form to post to the Index HttpPost action