Reputation: 123
I am trying to pass a Base64 string generated from an image back to my controller. I have a hidden input field on my view that binds to a property on my view model. However, when I return the view model to my controller, the PhotoPath property shows as null.
Not sure why the data is not binding. I use Javascript to set the value of the PhotoPath input:
$("#fileChooser").change(function(){
if (this.files && this.files[0]){
var reader = new FilerReader();
reader.onload = function (e) {
$("#PhotoPath").val(e.target.result);
}
reader.readAsDataURL(this.files[0]);
}
});
This is my input field:
@Html.HiddenFor(d => d.PhotoPath)
ViewModel property:
public string PhotoPath { get; set; }
If I pass in a regular string in an EditorFor instance, it passes to the controller just fine. Not really sure what the problem is. Any help would be appreciated!
EDIT: This is contained inside a template of a Kendo ListView if that provides anymore context.
EDIT 2: This is the inspector after I set the photo:
Upvotes: 0
Views: 1568
Reputation: 324
You can not use @Html or another html helper. Becouse html helpers is work one time(When page load). Because razor is not work after to page load. You should do it with a standart input type: hidden.
Upvotes: 0