25 Mar 2016

How to pass JS variable value from View to Controller

One of the easiest approach to handle this problem is

1. Add a new property into your Model.
2. Create a hidden input control in view , which maps to the newly created property of your Model.
3. Update the value of your hidden control in js function.

Let do it...

1.  Model
        public class Model: GenericEntityPoco
           // your property
        [HiddenInput(DisplayValue = false)]
        public DateTime FromTime { get; set; }

2.   In the view ( Create View)
          @Html.HiddenFor(model => model.FromTime)
          <input type="submit" value="Create" class="btn btn-primary btn-lg" onclick="setSlotSelected();" />

3. JS
    function setHiddenControl()
        document.getElementById('FromTime').value = "myValue to be updated";
        return true;