0

I'm trying to store a Func<T> or Expression<Func<T>> variable into a Html.HiddenFor(), inside a Razor view, in order to post it back to the controller, but it does not work as expected, as it's stored as a string, and comes back as null to the controller.

I need it to access particular properties of a model, and set them with other data passed to the controller at the same time.

I cannot find setter properties that I could store and use back in the controller to build the Func<object> variable (like I would with any complex object).

For instance, I send to the view, a model, containing a list of these:

new DataConflict<AppointmentEditModel, dynamic, dynamic>(x => x.StartDate, null, Start, appointment.StartDate)

then I store the lambda expression x => x.StartDate, which is a Expression<Func<T>>, like this (it seems to store it as a string):

@Html.HiddenFor(m => m.Appointment.ConflictList[i].DataProperty)

And when it comes back to the controller after submission, all the lambda containing properties are null.

I expect the controller to receive back the lambda expression containing object, it sent to the view.

Thank you.

Alex
  • 729
  • 2
  • 10
  • 24
  • 1
    All server values needs to be serializable so that it can be transmitted between client and server. A Func<> nor Expression is serializable. Not sure what the use case is here. But if you must do this you can store a string representing the desired logic and dynamically construct a Func<> based on your string. You will have to write your own code for this as Func<> are not serializable – Van Sep 23 '19 at 12:13
  • 1
    https://stackoverflow.com/questions/12960793/serialize-listfunct – Van Sep 23 '19 at 12:14

1 Answers1

0

As @Van has suggested, I've looked for a way to serialize the data, and I learned that this is not a good thing to do at all, to serialize lambda expressions.

I then have looked for another solution, and decided to use reflection, sending the member name to the client, and then back to the server.

Hope this help.

Alex
  • 729
  • 2
  • 10
  • 24