简体   繁体   中英

ASP.NET - Passing Base64 String From Razor View to Controller

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:

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM