0

When I use POST method on the below then I can see nulls in my Controller (when I hover over 'docs' while in debug mode). Why it happens? Both columns have values. I try to pass values from those fields to controller but nothing passes there.

View:

@using (Html.BeginForm("Update", "Home", FormMethod.Post))
{
<table>
  <tr>
    <td>
      @Html.HiddenFor(modelItem => item.Account) //it allows me send values for POST/GET methods
      @Html.DisplayFor(modelItem => item.Account)
    </td>
    <td class="choice">
      @Html.TextBoxFor(modelItem => item.PaymentDate, new { @type = "date", @class = "form-control datepicker" })
    </td>
    <td>
      @Html.ActionLink("Save", "Update", "Home", new { nrAccount = item.Account, manualDate = item.PaymentDate },null)
    </td>
  </tr>
</table>
}

Model:

public class DocumentsModel
   {
     public string Account { get; set; }
     public DateTime? PaymentDate { get; set; }
   }

Controller:

[HttpPost]
public ActionResult Update(DocumentsModel docs)
{
    //some code

    return RedirectToAction("Index");
}
Muska
  • 283
  • 4
  • 15
  • 1
    The following link might be helpful : https://stackoverflow.com/questions/11488442/asp-net-mvc-3-how-to-force-an-actionlink-to-do-a-httppost-instead-of-an-httpge – AntiqTech Oct 23 '20 at 23:20
  • 1
    Can you try this : @Html.ActionLink("Save", "Update", "Home", new { nrAccount = item.Account, manualDate = item.PaymentDate },new { @class="postLink"}) – AntiqTech Oct 23 '20 at 23:28

1 Answers1

1

The reason why your parameter in the post is null is because it cannot .NET cannot assume that the object you are sending is the actual object he is expecting, in this case what you should do is to tell it, that it will be in the body of the request, in order to make it work, you only need to add a Decorator to the signature called FromBody

[HttpPost]
public ActionResult Update([FromBody] DocumentsModel docs)
{
    //some code

    return RedirectToAction("Index");
}
nalnpir
  • 1,167
  • 6
  • 14
  • When I use [FromBody] then I need to use ```using System.Web.Http```, and change [HttpPost] to [System.Web.Mvc.HttpPost], don't know if it's ok. And one more question - why we don't neet to use [FromBody] here https://exceptionnotfound.net/asp-net-mvc-demystified-model-binding/ and many more other tutorials? I noticed that, based on various sources I went through, POST usually works fine without [FromBody] – Muska Oct 24 '20 at 12:48
  • or here https://www.youtube.com/watch?v=cRWyEtMFDeg, they don't use [FromBody] and data is extracted properly form the fields – Muska Oct 24 '20 at 13:10
  • I think I found a solution. I have about 100 rows in my web form. To bind data and post to server I think I need to use for loop, not for each. Look here: https://stackoverflow.com/questions/14165632/asp-net-mvc-4-for-loop-posts-model-collection-properties-but-foreach-does-not – Muska Oct 24 '20 at 13:55
  • Sorry for the delay, but for what i seen in both of the examples you sent me, they re not complex types, they re just primitives, whenever you want to deserialize a class you would need to specify the class or tell the compiler to infer it from the body of the message – nalnpir Oct 24 '20 at 22:55
  • They are complex types, my controller takes object as an argument. Ok thanks, I'll try to figure it out – Muska Oct 25 '20 at 13:22