Kashi Badiger
Kashi Badiger

Reputation: 3

getting null value in enddate parameter after passing setDate value to controller action method's enddate parameter

Javascript code

function setDate(id) {

        debugger;
        $('#doctorId').val(id.name);
        $('#hhduration').val(id.id);
        var datesArray = [];
        var d = new Date();
        var startdate = d.toLocaleString();
        var ed = new Date();
        ed.setDate(d.getDate() + 12);
        var Enddate = ed.toLocaleString();
        var doctorId = id.name;

        $.ajax({
            type: "POST",
            url: '@Url.Action("GetAvailabledates", "Home")',
            data: { startdate: startdate, enddate: Enddate, doctorid: doctorId },
            datatype: "json",
            async: false
        }).done(function (data) {

            try {
                if (data.length > 0) {
                    debugger;
                    datesArray = data;
                }



            }
            catch (e) {

            }
        });

MVC Controller method

        [HttpPost]
        public ActionResult GetAvailabledates(DateTime startdate,DateTime? enddate,string doctorid)
        {
            int[] dates = null;
            using (DepartmentOperations dos = new DepartmentOperations())
            {
                 dates = dos.GetAvailabledates(startdate, enddate, doctorid);
            }
            return Json(dates, JsonRequestBehavior.AllowGet);
        }

Upvotes: 0

Views: 473

Answers (2)

Saif Ur Rehman
Saif Ur Rehman

Reputation: 1

If you're experiencing issues with the current approach and would like to try a different method for passing the enddate parameter, you could consider using a JSON payload in your POST request. Here's an example of how you can modify your controller action method and the way you send data to it:

public ActionResult GetAvailabledates([FromBody] DateInputModel dateInput)
{
    int[] dates = null;
    using (DepartmentOperations dos = new DepartmentOperations())
    {
        dates = dos.GetAvailabledates(dateInput.StartDate, dateInput.EndDate, dateInput.DoctorId);
    }
    return Json(dates, JsonRequestBehavior.AllowGet);
}

public class DateInputModel
{
    public DateTime StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public string DoctorId { get; set; }
}

Upvotes: 0

Divyesh Jani
Divyesh Jani

Reputation: 311

Use Json Signify make Enddate not null if you do not pass date then it will pass as '0001-01-01 00:00:00'

one more thing if you want to set nullable thing then it should be last in parameter of the controller

https://cmatskas.com/asp-net-mvc-webapi-optional-parameters/

Upvotes: 0

Related Questions