Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Edgar Arroyo 19 posts 161 karma points
    May 18, 2019 @ 19:42
    Edgar Arroyo
    0

    Need help adding an image from the MEDIA folder to a partial view

    I need help :-)

    Here is my partial view:

    @model MySite.Models.MyModel
    @using (Html.BeginUmbracoForm<MySite.Controllers.MySurfaceController>("SubmitForm", "My", FormMethod.Post))
    {
      **I want to add image here**
    exampe:  <img src="@???" />
    }
    
  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    May 19, 2019 @ 07:45
    Marc Goodson
    100

    Hi Edgar

    I think if you make your Partial View inherit UmbracoViewPage

    @inherits UmbracoViewPage<MySite.Models.MyModel>
    @{
    // I'm not sure how you are deciding which image to show.. eg on a picker?
    myImage = Umbraco.Media(123);
    }
    @using (Html.BeginUmbracoForm<MySite.Controllers.MySurfaceController>("SubmitForm", "My", FormMethod.Post))
    {
      **I want to add image here**
    if (myImage!=mull){
    exampe:  <img src="@myImage.Url" />
    }
    }
    

    or something similar.

    regards

    Marc

  • Edgar Arroyo 19 posts 161 karma points
    May 20, 2019 @ 03:18
    Edgar Arroyo
    0

    Thank you for your reply. I got an error:

    Server Error in '/' Application.
    Compilation Error
    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 
    
    Compiler Error Message: CS1003: Syntax error, ',' expected
    
    Source Error:
    
    
    Line 34:     
    Line 35:     
    **Line 36:     public class _Page_Views_Partials_MyPartialView_cshtml : UmbracoViewPage<model MySite.Models.MyModel> {**
    Line 37:         
    Line 38: #line hidden
    
  • Edgar Arroyo 19 posts 161 karma points
    May 20, 2019 @ 03:34
    Edgar Arroyo
    0

    Ok, I removed "model" from the inherit, a typo maybe?

    But.... When I added @Html.AntiForgeryToken() it gave me an error...

    Server Error in '/' Application.
    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    
    Source Error: 
    
    
    Line 5: @using (Html.BeginUmbracoForm<MySite.Controllers.MySurfaceController>("SubmitForm", "My", FormMethod.Post))
    Line 6:  {
    **Line 7:      @Html.AntiForgeryToken()**
    Line 8:     **I want to add image here**
    Line 9:     if (myImage!=mull){
    

    The error is on line 7 (bold doesn't seem to work inside the code sample...

    EDIT: Never mind, it worked. I forgot to change the ID for the media ... whoops... :-D

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    May 20, 2019 @ 08:10
    Marc Goodson
    1

    Hi Edgar

    Sorry for the typo! in the <> brackets.

    Glad you got it working.

    regards

    Marc

  • Edgar Arroyo 19 posts 161 karma points
    May 20, 2019 @ 14:33
    Edgar Arroyo
    0

    Thank you.

    One last question. How can I find the MediaID using it's "name" or any other property? That way I can just use the "name" of the image....

    Thanks.

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    May 20, 2019 @ 18:37
    Marc Goodson
    1

    Hi Edgar

    I think I understand you correctly... but the Partial View will be on a page in Umbraco somewhere, so I think on the document type for that page - or if this is a 'global image' on the document type for the homepage - create a new property called 'Form Image' or similar and make the property a 'Media Picker'

    Then in the backoffice of Umbraco 'pick' the image that you want to display on the form and publish the page.

    Then in your Partial View you can read the picked image and display it accordingly eg

    @inherits UmbracoViewPage<MySite.Models.MyModel>
    @{
     var formImage = Model.Value<IPublishedContent>("formImage", fallback: Fallback.Ancestors);
    
    }
    @using (Html.BeginUmbracoForm<MySite.Controllers.MySurfaceController>("SubmitForm", "My", FormMethod.Post))
    {
      **I want to add image here**
    if (formImage!=mull){
    <img src="@formImage.Url" />
    }
    }
    

    the fallback to ancestors, means Umbraco will look for a media picker with alias "formImage" all the way up the content tree to the homepage - so if the image is the same across all forms, you could set it globally on home.

    regards

    Marc

  • Edgar Arroyo 19 posts 161 karma points
    May 20, 2019 @ 18:54
    Edgar Arroyo
    0

    Thanks.

    Error, Model doesn't have a definition for 'Value' (typo maybe?)

    EDIT: Here is the exact error, it might help.

    'MyModel' does not contain a definition for 'Value' and the best extension method overload 'PublishedElementExtensions.Value<IPublishedContent>(IPublishedElement, string, string, string, Fallback, IPublishedContent)' requires a receiver of type 'IPublishedElement'
    
Please Sign in or register to post replies

Write your reply to:

Draft