user6815384
user6815384

Reputation: 219

datetimepicker not passing value to Controller

I have this input date from the MVC form that I wanted to have the datetimepicker value pass to controller and it is not working. Not sure what I missed here.

View add.cshtml

<form id="msform" class="form-inline" method="post">
<div class="form-group">
    <div class='input-group date' id="txtReviewDate">
        <input type="text" class="form-control"/>
        <span class="input-group-addon">
            <span class="glyphicon glyphicon-calendar"></span>
        </span>
    </div>
</div>

<script type="text/javascript">
    $(function () {
        $('txtReviewDate').datetimepicker({
            format: 'MM/DD/YYYY',
            defaultDate:new Date()
        });
    });
</script>
 </form>

Model

public Nullable<DateTime> tempReviewDate

Controller code

[HttpPost]
public ActionResult Add (calfreshByWorkUnitID customerinfo)
{
    var vartempReviewDate = Convert.ToDateTime(Request["txtReviewDate"].ToString());
}

Upvotes: 1

Views: 2384

Answers (1)

user3559349
user3559349

Reputation:

You have an input that does not have a name attribute so it does not submit a value. A form only submits the name/value pairs of its successful form controls.

Change your view to to bind to your model (note I'm assuming the model you have shown is named calfreshByWorkUnitID)

@model calfreshByWorkUnitID
....
@using (Html.BeginForm(...))
{
    ....
    @Html.TextBoxFor(m => m.tempReviewDate, new { @class="form-control" })

Script (attach the plugin to your input)

$('#tempReviewDate').datetimepicker({
    format: 'MM/DD/YYYY',
    defaultDate:new Date()
});

and then in the POST method, your model will be correctly bound with the selected date

[HttpPost]
public ActionResult Add (calfreshByWorkUnitID customerinfo)
{
    // customerinfo.tempReviewDate contains the selected value

Upvotes: 3

Related Questions