David
David

Reputation: 17

Send post from view to controller

I'm new in Asp.Net, I trying to do a post from view to controller like:

@model Models.CapturaViewModel
  @using(Html.BeginForm("EnviarIncrementos", "Controller", FormMethod.Post, htmlAttributes: new { @class = "table-bordered" }))
 {
<div class="form-group">
  <div class="col-md-12">
    <div class="col-md-3">
      Fecha de movimiento:
    </div>
    <div class='col-md-4'>
      <div class="form-group">
        <div class='input-group date' id='datetimepicker1'>
          <input type='text' class="form-control" />
          <span class="input-group-addon">
             <span class="glyphicon glyphicon-calendar"></span>
          </span>
        </div>
      </div>
    </div>
    <div class="col-md-2 col-md-push-3">
      <input type="submit" value="Guardar" class="btn btn-xs" style="background-color:#3399FF;color:#FFFFFF" />
    </div>
  </div>
</div>
}

As you can see I have a date picker there. I want to know how can I receive value from that date picker in controller method:

[HttpPost]
public ActionResult EnviarIncrementos()
        {
            return RedirectToAction("Index");
        }

Help is very appreciatted. Regards

Upvotes: 0

Views: 61

Answers (1)

penleychan
penleychan

Reputation: 5470

Your .cshtml for input require a name attribute to pass the data to your Controller via POST.

Don't know what property CapturaViewModel have but lets assume you have a property public DateTime DatePicker { get; set; }

With that, now you need to modify your .cshtml where

<input type='text' class="form-control" /> 

to

<input type='text' name="DatePicker" class="form-control" />

or via using HtmlHelper

@Html.TextBoxFor(m => m.DatePicker, new { @class = "form-control" })

Lastly your controller is missing parameter that accepts CapturaViewModel. Change your controller to:

[HttpPost]
public ActionResult EnviarIncrementos(CapturaViewModel model)
{
    return RedirectToAction("Index");
}

Upvotes: 1

Related Questions