Returning null in all parameters in Controller

public class MyLeave
{
    
    public int Leave_id { get; set; }

    public string EmpID { get; set; }

    [Display(Name = "From :")]
    //[DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}", ApplyFormatInEditMode = true)]
    [Required]
    public DateTime FromDate { get; set; }

    [Display(Name = "Till :")]
    //[DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}", ApplyFormatInEditMode = true)]
    [Required]
    public DateTime TillDate { get; set; }

    [Display(Name = "Total Number Of Days :")]
    [Required]
    public int TotalDays { get; set; }

    [Display(Name = "Reason :")]
    [Required]
    public string Reason { get; set; }

    public string Status { get; set; }

    public string Comment { get; set; }



   public List<MyLeave> LeaveData { get; set; }

}

public class LeaveManagementController : Controller { // GET: LeaveManagement

    public ActionResult ApplyLeave()
    {
        
        return View();
    }

    public ActionResult SubmitLeave(MyLeave obj)
    {
       
        string SessionEmployeeID = Session["EmployeeID"].ToString();
        string con = System.Configuration.ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
        SqlConnection cnn = new SqlConnection(con);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = cnn;
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.CommandText = "sp_Leave";
        cmd.Parameters.AddWithValue("@EmpID", SessionEmployeeID); 
        cmd.Parameters.AddWithValue("@FromDate", obj.FromDate); null
        cmd.Parameters.AddWithValue("@TillDate", obj.TillDate); null
        cmd.Parameters.AddWithValue("@TotalDays", obj.TotalDays);
        cmd.Parameters.AddWithValue("@Reason", obj.Reason);

        cnn.Open();
        cmd.ExecuteNonQuery();
        cnn.Close();

        TempData["LeaveAppMessage"] = "Application Submitted To Admin Successfully!";

        return RedirectToAction("ApplyLeave");

    }
}

@model WebTimeSheetManagement.Models.MyLeave

@using (Html.BeginForm("MyLeave","LeaveManagement")) {

<div>
    @Html.LabelFor(model => model.FromDate, htmlAttributes: new { @class = "control-label" })
   @Html.EditorFor(model => model.FromDate, new { htmlAttributes = new { @class = "datepicker form-control", value = "myFromDate", id = "datepicker1", required = "required" } })
    @Html.ValidationMessageFor(model => model.FromDate)
</div>

<div>
    @Html.LabelFor(model => model.TillDate, htmlAttributes: new { @class = "control-label " })
    @Html.EditorFor(model => model.TillDate, new { htmlAttributes = new { @class = "datepicker form-control", value = "myTillDate", id = "datepicker2", required = "required" } })
    @Html.ValidationMessageFor(model => model.TillDate)
</div>

<br />

<div>

    @Html.LabelFor(model => model.TotalDays, htmlAttributes: new { @class = "control-label " })
    @Html.TextBoxFor(model => model.TotalDays, new { @class = "form-control", @maxlength = 40, id = "Calculate", required = "required" })
    @Html.ValidationMessageFor(model => model.TotalDays)
</div>


<br />
<div class="form-group ">
    @Html.LabelFor(model => model.Reason, htmlAttributes: new { @class = "control-label" })
    @Html.TextAreaFor(model => model.Reason, new { @class = "form-control", @onkeypress = "", required = "required", col = 4, row = 10})
    @Html.ValidationMessageFor(model => model.Reason)
</div>

<br />

<input type="submit" class="btn btn-success" id="btn_apply" value="Apply" onclick="location.href='@Url.Action("SubmitLeave", "LeaveManagement","")'" />

}

date picker return null reason also return null please help as soon as possible stuck from 2-3 days date picker returns date like 01/01/0001



Read more here: https://stackoverflow.com/questions/64403681/returning-null-in-all-parameters-in-controller

Content Attribution

This content was originally published by Developer_Raghu at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: