0

I have the following code in my View:

<td class="choice">
       @Html.TextBoxFor(modelItem => item.ManualDate, new { @type = "date", @class = "form-control datepicker" })
</td>

In my database ManualDate field is a column with nulls mostly. So in my web app in this field nulls are shown (no value).

As you can see it's a 'date' input field. User can choose date in the datepicker in this field. I want to save this date in a database. How can I retrieve value (date picked by user) from this field and pass it to controller?

Muska
  • 283
  • 4
  • 15
  • I assume your model has other properties too, and they get to the controller just fine. What makes this one special? – GSerg Oct 22 '20 at 18:32
  • Yes, it has. This one is special because initially all values are null in this column and user can choose date on a datepicker in this column. And I want this value to be saved in db but when I pass value from item.ManualDate to controller then null value is returned (even though user chose date). – Muska Oct 22 '20 at 18:35
  • 1
    The HTML helpers (such as `TextBoxFor`) and the model binder are not concerned with databases. If there is a valid control in HTML, it gets to the controller. If that control has a valid name, its value gets into respective place in the model, otherwise that place in the model remains null. If your model object does not have this value the moment your controller method is called by the framework, it's already too late to think about databases. From your lambda I assume you have a list of child items in the model, if so please see https://stackoverflow.com/q/25333332/11683. – GSerg Oct 22 '20 at 18:39
  • modelItem => item.MamualDate seems wrong. Did you mean to have item => ... ? – Tobias Kildetoft Oct 22 '20 at 18:43
  • @TobiasKildetoft that makes sense, I tried this: ```@Html.ActionLink("Save", "Update", "Home", new { nrCust = item.Account, manualDate = item.ManualDate }, null)``` to pass values to controller. So that means I should use 'id' of thts razor element and pass its value to controller (the that would be date? )? – Muska Oct 22 '20 at 18:58
  • 1
  • I used ```
    ```. And in this ```
    ``` I also have ActionLink. In this ActionLink I pass values (as you can see in my previous comment). I also see that e.g. item.Account is passed to controller correctly (so it passes value from this field). My main problem is - how to pass date picked by user to controller? This date is being picked in field ManualDate. I really don't know how to do this, have no idea. I can't go further with my project because of that (pls note I'm still learning mvc). Could you give me a short, live example?
    – Muska Oct 23 '20 at 07:37
  • You cannot use actionlink to pass values in a form. If `item.Account` is passed correctly, it's not because of the actionlink, it's because somewhere else in the form you have a field for it. – GSerg Oct 23 '20 at 11:09
  • What does the datepicker put into the textbox when a date is chosen? – Dave Barnett Oct 23 '20 at 19:29
  • It just put a date. You choose date on a datepicker and date is put into this textbox (note that this textbox is of type date) – Muska Oct 23 '20 at 19:50

0 Answers0