How to reference controller string in HTML





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am trying to display my controller string in the src of the img tag, but yes I am failing.



Here is my Model code:



public class PigeonDetails
{
[Key]
public int PigeonID { get; set; }

[Required]
[Display(Name = "Ring Number")]
public string RingNumber { get; set; }

[Display(Name = "Pigeon Name")]
public string Name { get; set; }

[Required]
[Display(Name = "Pigeon Color")]
public string Color { get; set; }

[Required]
[Display(Name = "Gender")]
public string Sex { get; set; }

[Required]
[Display(Name = "Year Type")]
public int Year { get; set; }

[Display(Name = "Description")]
public string Description { get; set; }

[Display(Name = "Date Added")]
public DateTime DateAdded { get; set; }

public string Image { get; set; }

}


Here is my Controller - Only the method i use:



[HttpGet]
// GET: PigeonDetails/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}

var pigeonDetails = await _context.PigeonDetails.SingleOrDefaultAsync(m => m.PigeonID == id);

string placeholder = _context.PigeonDetails.Where(u => u.PigeonID == id).Select(u => u.Image).ToString();
pigeonDetails.Image = placeholder;
if (pigeonDetails == null)
{
return NotFound();
}
ViewData["ImagePath"] = pigeonDetails;
return View(pigeonDetails);
}


The pigeonDetails.Image is a string stored in the db (image path - and it stores correctly)



here is my html:



  @{var image = (PigeonDetails)ViewData["ImagePath"];}

<img src="@image.Image" id="profile" alt="user" class="img-responsive radius">


It does not display my image on the page, please help.










share|improve this question

























  • try to set ViewData["ImagePath"] = pigeonDetails; just before the return View(pigeonDetails);

    – er-sho
    Nov 22 '18 at 11:55











  • I did, yet no luck .

    – stefan van schoor
    Nov 22 '18 at 11:59











  • could you please show your full code of controller

    – er-sho
    Nov 22 '18 at 12:01











  • There it is, i edited the post

    – stefan van schoor
    Nov 22 '18 at 12:06











  • did u specify any @model on top of view?

    – er-sho
    Nov 22 '18 at 12:08


















0















I am trying to display my controller string in the src of the img tag, but yes I am failing.



Here is my Model code:



public class PigeonDetails
{
[Key]
public int PigeonID { get; set; }

[Required]
[Display(Name = "Ring Number")]
public string RingNumber { get; set; }

[Display(Name = "Pigeon Name")]
public string Name { get; set; }

[Required]
[Display(Name = "Pigeon Color")]
public string Color { get; set; }

[Required]
[Display(Name = "Gender")]
public string Sex { get; set; }

[Required]
[Display(Name = "Year Type")]
public int Year { get; set; }

[Display(Name = "Description")]
public string Description { get; set; }

[Display(Name = "Date Added")]
public DateTime DateAdded { get; set; }

public string Image { get; set; }

}


Here is my Controller - Only the method i use:



[HttpGet]
// GET: PigeonDetails/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}

var pigeonDetails = await _context.PigeonDetails.SingleOrDefaultAsync(m => m.PigeonID == id);

string placeholder = _context.PigeonDetails.Where(u => u.PigeonID == id).Select(u => u.Image).ToString();
pigeonDetails.Image = placeholder;
if (pigeonDetails == null)
{
return NotFound();
}
ViewData["ImagePath"] = pigeonDetails;
return View(pigeonDetails);
}


The pigeonDetails.Image is a string stored in the db (image path - and it stores correctly)



here is my html:



  @{var image = (PigeonDetails)ViewData["ImagePath"];}

<img src="@image.Image" id="profile" alt="user" class="img-responsive radius">


It does not display my image on the page, please help.










share|improve this question

























  • try to set ViewData["ImagePath"] = pigeonDetails; just before the return View(pigeonDetails);

    – er-sho
    Nov 22 '18 at 11:55











  • I did, yet no luck .

    – stefan van schoor
    Nov 22 '18 at 11:59











  • could you please show your full code of controller

    – er-sho
    Nov 22 '18 at 12:01











  • There it is, i edited the post

    – stefan van schoor
    Nov 22 '18 at 12:06











  • did u specify any @model on top of view?

    – er-sho
    Nov 22 '18 at 12:08














0












0








0








I am trying to display my controller string in the src of the img tag, but yes I am failing.



Here is my Model code:



public class PigeonDetails
{
[Key]
public int PigeonID { get; set; }

[Required]
[Display(Name = "Ring Number")]
public string RingNumber { get; set; }

[Display(Name = "Pigeon Name")]
public string Name { get; set; }

[Required]
[Display(Name = "Pigeon Color")]
public string Color { get; set; }

[Required]
[Display(Name = "Gender")]
public string Sex { get; set; }

[Required]
[Display(Name = "Year Type")]
public int Year { get; set; }

[Display(Name = "Description")]
public string Description { get; set; }

[Display(Name = "Date Added")]
public DateTime DateAdded { get; set; }

public string Image { get; set; }

}


Here is my Controller - Only the method i use:



[HttpGet]
// GET: PigeonDetails/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}

var pigeonDetails = await _context.PigeonDetails.SingleOrDefaultAsync(m => m.PigeonID == id);

string placeholder = _context.PigeonDetails.Where(u => u.PigeonID == id).Select(u => u.Image).ToString();
pigeonDetails.Image = placeholder;
if (pigeonDetails == null)
{
return NotFound();
}
ViewData["ImagePath"] = pigeonDetails;
return View(pigeonDetails);
}


The pigeonDetails.Image is a string stored in the db (image path - and it stores correctly)



here is my html:



  @{var image = (PigeonDetails)ViewData["ImagePath"];}

<img src="@image.Image" id="profile" alt="user" class="img-responsive radius">


It does not display my image on the page, please help.










share|improve this question
















I am trying to display my controller string in the src of the img tag, but yes I am failing.



Here is my Model code:



public class PigeonDetails
{
[Key]
public int PigeonID { get; set; }

[Required]
[Display(Name = "Ring Number")]
public string RingNumber { get; set; }

[Display(Name = "Pigeon Name")]
public string Name { get; set; }

[Required]
[Display(Name = "Pigeon Color")]
public string Color { get; set; }

[Required]
[Display(Name = "Gender")]
public string Sex { get; set; }

[Required]
[Display(Name = "Year Type")]
public int Year { get; set; }

[Display(Name = "Description")]
public string Description { get; set; }

[Display(Name = "Date Added")]
public DateTime DateAdded { get; set; }

public string Image { get; set; }

}


Here is my Controller - Only the method i use:



[HttpGet]
// GET: PigeonDetails/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}

var pigeonDetails = await _context.PigeonDetails.SingleOrDefaultAsync(m => m.PigeonID == id);

string placeholder = _context.PigeonDetails.Where(u => u.PigeonID == id).Select(u => u.Image).ToString();
pigeonDetails.Image = placeholder;
if (pigeonDetails == null)
{
return NotFound();
}
ViewData["ImagePath"] = pigeonDetails;
return View(pigeonDetails);
}


The pigeonDetails.Image is a string stored in the db (image path - and it stores correctly)



here is my html:



  @{var image = (PigeonDetails)ViewData["ImagePath"];}

<img src="@image.Image" id="profile" alt="user" class="img-responsive radius">


It does not display my image on the page, please help.







c# html asp.net-core






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 12:36









Matt

473416




473416










asked Nov 22 '18 at 11:51









stefan van schoorstefan van schoor

75




75













  • try to set ViewData["ImagePath"] = pigeonDetails; just before the return View(pigeonDetails);

    – er-sho
    Nov 22 '18 at 11:55











  • I did, yet no luck .

    – stefan van schoor
    Nov 22 '18 at 11:59











  • could you please show your full code of controller

    – er-sho
    Nov 22 '18 at 12:01











  • There it is, i edited the post

    – stefan van schoor
    Nov 22 '18 at 12:06











  • did u specify any @model on top of view?

    – er-sho
    Nov 22 '18 at 12:08



















  • try to set ViewData["ImagePath"] = pigeonDetails; just before the return View(pigeonDetails);

    – er-sho
    Nov 22 '18 at 11:55











  • I did, yet no luck .

    – stefan van schoor
    Nov 22 '18 at 11:59











  • could you please show your full code of controller

    – er-sho
    Nov 22 '18 at 12:01











  • There it is, i edited the post

    – stefan van schoor
    Nov 22 '18 at 12:06











  • did u specify any @model on top of view?

    – er-sho
    Nov 22 '18 at 12:08

















try to set ViewData["ImagePath"] = pigeonDetails; just before the return View(pigeonDetails);

– er-sho
Nov 22 '18 at 11:55





try to set ViewData["ImagePath"] = pigeonDetails; just before the return View(pigeonDetails);

– er-sho
Nov 22 '18 at 11:55













I did, yet no luck .

– stefan van schoor
Nov 22 '18 at 11:59





I did, yet no luck .

– stefan van schoor
Nov 22 '18 at 11:59













could you please show your full code of controller

– er-sho
Nov 22 '18 at 12:01





could you please show your full code of controller

– er-sho
Nov 22 '18 at 12:01













There it is, i edited the post

– stefan van schoor
Nov 22 '18 at 12:06





There it is, i edited the post

– stefan van schoor
Nov 22 '18 at 12:06













did u specify any @model on top of view?

– er-sho
Nov 22 '18 at 12:08





did u specify any @model on top of view?

– er-sho
Nov 22 '18 at 12:08












3 Answers
3






active

oldest

votes


















0














Can you try this



upper page



@model ModelNameSpace.PigeonDetails


and your html element



<img src="@Model.Image" id="profile" alt="user" class="img-responsive radius">


Please change namespace. This is just example namespace name ;)






share|improve this answer































    0














    First of all, remove string placeholder because pigeonDetails already has it if it matches the id. Also there is an issue with .ToString() on IQueryable as it will return the fully qualified name of the runtime type of the object that received the call.



    Second, Remove ViewData bit. You already have Model for your view (better if you use viewmodel instead).



    Then add @model PigeonDetails on top of your view to expose Model property for your view.



    Finally, <img src="@Model.Image" />






    share|improve this answer

































      0














      This is how I would have done it. I removed a lot of unnecessary code.



      I trimmed your Edit action method quite a bit:



      [HttpGet]
      public async Task<IActionResult> Edit(int? id)
      {
      if (id == null)
      {
      return NotFound();
      }

      PigeonDetails pigeonDetails = await _context.PigeonDetails
      .Where(m => m.PigeonID == id)
      .SingleOrDefaultAsync();

      if (pigeonDetails == null)
      {
      return NotFound();
      }

      return View(pigeonDetails);
      }


      In your Edit view page do the following:



      @model YourNamespace.PigeonDetails

      <img src="@Model.Image" id="profile" alt="user" class="img-responsive radius" />


      Stay clear of view data, the preferred and safer way is to use a view model.



      I hope this helps you.






      share|improve this answer


























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430412%2fhow-to-reference-controller-string-in-html-img-src%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        Can you try this



        upper page



        @model ModelNameSpace.PigeonDetails


        and your html element



        <img src="@Model.Image" id="profile" alt="user" class="img-responsive radius">


        Please change namespace. This is just example namespace name ;)






        share|improve this answer




























          0














          Can you try this



          upper page



          @model ModelNameSpace.PigeonDetails


          and your html element



          <img src="@Model.Image" id="profile" alt="user" class="img-responsive radius">


          Please change namespace. This is just example namespace name ;)






          share|improve this answer


























            0












            0








            0







            Can you try this



            upper page



            @model ModelNameSpace.PigeonDetails


            and your html element



            <img src="@Model.Image" id="profile" alt="user" class="img-responsive radius">


            Please change namespace. This is just example namespace name ;)






            share|improve this answer













            Can you try this



            upper page



            @model ModelNameSpace.PigeonDetails


            and your html element



            <img src="@Model.Image" id="profile" alt="user" class="img-responsive radius">


            Please change namespace. This is just example namespace name ;)







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 12:35









            Reha BayarReha Bayar

            11




            11

























                0














                First of all, remove string placeholder because pigeonDetails already has it if it matches the id. Also there is an issue with .ToString() on IQueryable as it will return the fully qualified name of the runtime type of the object that received the call.



                Second, Remove ViewData bit. You already have Model for your view (better if you use viewmodel instead).



                Then add @model PigeonDetails on top of your view to expose Model property for your view.



                Finally, <img src="@Model.Image" />






                share|improve this answer






























                  0














                  First of all, remove string placeholder because pigeonDetails already has it if it matches the id. Also there is an issue with .ToString() on IQueryable as it will return the fully qualified name of the runtime type of the object that received the call.



                  Second, Remove ViewData bit. You already have Model for your view (better if you use viewmodel instead).



                  Then add @model PigeonDetails on top of your view to expose Model property for your view.



                  Finally, <img src="@Model.Image" />






                  share|improve this answer




























                    0












                    0








                    0







                    First of all, remove string placeholder because pigeonDetails already has it if it matches the id. Also there is an issue with .ToString() on IQueryable as it will return the fully qualified name of the runtime type of the object that received the call.



                    Second, Remove ViewData bit. You already have Model for your view (better if you use viewmodel instead).



                    Then add @model PigeonDetails on top of your view to expose Model property for your view.



                    Finally, <img src="@Model.Image" />






                    share|improve this answer















                    First of all, remove string placeholder because pigeonDetails already has it if it matches the id. Also there is an issue with .ToString() on IQueryable as it will return the fully qualified name of the runtime type of the object that received the call.



                    Second, Remove ViewData bit. You already have Model for your view (better if you use viewmodel instead).



                    Then add @model PigeonDetails on top of your view to expose Model property for your view.



                    Finally, <img src="@Model.Image" />







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 22 '18 at 13:00

























                    answered Nov 22 '18 at 12:45









                    KrishnaDhunganaKrishnaDhungana

                    1,76431829




                    1,76431829























                        0














                        This is how I would have done it. I removed a lot of unnecessary code.



                        I trimmed your Edit action method quite a bit:



                        [HttpGet]
                        public async Task<IActionResult> Edit(int? id)
                        {
                        if (id == null)
                        {
                        return NotFound();
                        }

                        PigeonDetails pigeonDetails = await _context.PigeonDetails
                        .Where(m => m.PigeonID == id)
                        .SingleOrDefaultAsync();

                        if (pigeonDetails == null)
                        {
                        return NotFound();
                        }

                        return View(pigeonDetails);
                        }


                        In your Edit view page do the following:



                        @model YourNamespace.PigeonDetails

                        <img src="@Model.Image" id="profile" alt="user" class="img-responsive radius" />


                        Stay clear of view data, the preferred and safer way is to use a view model.



                        I hope this helps you.






                        share|improve this answer






























                          0














                          This is how I would have done it. I removed a lot of unnecessary code.



                          I trimmed your Edit action method quite a bit:



                          [HttpGet]
                          public async Task<IActionResult> Edit(int? id)
                          {
                          if (id == null)
                          {
                          return NotFound();
                          }

                          PigeonDetails pigeonDetails = await _context.PigeonDetails
                          .Where(m => m.PigeonID == id)
                          .SingleOrDefaultAsync();

                          if (pigeonDetails == null)
                          {
                          return NotFound();
                          }

                          return View(pigeonDetails);
                          }


                          In your Edit view page do the following:



                          @model YourNamespace.PigeonDetails

                          <img src="@Model.Image" id="profile" alt="user" class="img-responsive radius" />


                          Stay clear of view data, the preferred and safer way is to use a view model.



                          I hope this helps you.






                          share|improve this answer




























                            0












                            0








                            0







                            This is how I would have done it. I removed a lot of unnecessary code.



                            I trimmed your Edit action method quite a bit:



                            [HttpGet]
                            public async Task<IActionResult> Edit(int? id)
                            {
                            if (id == null)
                            {
                            return NotFound();
                            }

                            PigeonDetails pigeonDetails = await _context.PigeonDetails
                            .Where(m => m.PigeonID == id)
                            .SingleOrDefaultAsync();

                            if (pigeonDetails == null)
                            {
                            return NotFound();
                            }

                            return View(pigeonDetails);
                            }


                            In your Edit view page do the following:



                            @model YourNamespace.PigeonDetails

                            <img src="@Model.Image" id="profile" alt="user" class="img-responsive radius" />


                            Stay clear of view data, the preferred and safer way is to use a view model.



                            I hope this helps you.






                            share|improve this answer















                            This is how I would have done it. I removed a lot of unnecessary code.



                            I trimmed your Edit action method quite a bit:



                            [HttpGet]
                            public async Task<IActionResult> Edit(int? id)
                            {
                            if (id == null)
                            {
                            return NotFound();
                            }

                            PigeonDetails pigeonDetails = await _context.PigeonDetails
                            .Where(m => m.PigeonID == id)
                            .SingleOrDefaultAsync();

                            if (pigeonDetails == null)
                            {
                            return NotFound();
                            }

                            return View(pigeonDetails);
                            }


                            In your Edit view page do the following:



                            @model YourNamespace.PigeonDetails

                            <img src="@Model.Image" id="profile" alt="user" class="img-responsive radius" />


                            Stay clear of view data, the preferred and safer way is to use a view model.



                            I hope this helps you.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 22 '18 at 13:14

























                            answered Nov 22 '18 at 13:03









                            Brendan VogtBrendan Vogt

                            15.8k29121211




                            15.8k29121211






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430412%2fhow-to-reference-controller-string-in-html-img-src%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Guess what letter conforming each word

                                Port of Spain

                                Run scheduled task as local user group (not BUILTIN)