How to reference controller string in HTML











up vote
0
down vote

favorite












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-shoaib
    Nov 22 at 11:55










  • I did, yet no luck .
    – stefan van schoor
    Nov 22 at 11:59










  • could you please show your full code of controller
    – er-shoaib
    Nov 22 at 12:01










  • There it is, i edited the post
    – stefan van schoor
    Nov 22 at 12:06










  • did u specify any @model on top of view?
    – er-shoaib
    Nov 22 at 12:08















up vote
0
down vote

favorite












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-shoaib
    Nov 22 at 11:55










  • I did, yet no luck .
    – stefan van schoor
    Nov 22 at 11:59










  • could you please show your full code of controller
    – er-shoaib
    Nov 22 at 12:01










  • There it is, i edited the post
    – stefan van schoor
    Nov 22 at 12:06










  • did u specify any @model on top of view?
    – er-shoaib
    Nov 22 at 12:08













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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 at 12:36









Matt

176111




176111










asked Nov 22 at 11:51









stefan van schoor

75




75












  • try to set ViewData["ImagePath"] = pigeonDetails; just before the return View(pigeonDetails);
    – er-shoaib
    Nov 22 at 11:55










  • I did, yet no luck .
    – stefan van schoor
    Nov 22 at 11:59










  • could you please show your full code of controller
    – er-shoaib
    Nov 22 at 12:01










  • There it is, i edited the post
    – stefan van schoor
    Nov 22 at 12:06










  • did u specify any @model on top of view?
    – er-shoaib
    Nov 22 at 12:08


















  • try to set ViewData["ImagePath"] = pigeonDetails; just before the return View(pigeonDetails);
    – er-shoaib
    Nov 22 at 11:55










  • I did, yet no luck .
    – stefan van schoor
    Nov 22 at 11:59










  • could you please show your full code of controller
    – er-shoaib
    Nov 22 at 12:01










  • There it is, i edited the post
    – stefan van schoor
    Nov 22 at 12:06










  • did u specify any @model on top of view?
    – er-shoaib
    Nov 22 at 12:08
















try to set ViewData["ImagePath"] = pigeonDetails; just before the return View(pigeonDetails);
– er-shoaib
Nov 22 at 11:55




try to set ViewData["ImagePath"] = pigeonDetails; just before the return View(pigeonDetails);
– er-shoaib
Nov 22 at 11:55












I did, yet no luck .
– stefan van schoor
Nov 22 at 11:59




I did, yet no luck .
– stefan van schoor
Nov 22 at 11:59












could you please show your full code of controller
– er-shoaib
Nov 22 at 12:01




could you please show your full code of controller
– er-shoaib
Nov 22 at 12:01












There it is, i edited the post
– stefan van schoor
Nov 22 at 12:06




There it is, i edited the post
– stefan van schoor
Nov 22 at 12:06












did u specify any @model on top of view?
– er-shoaib
Nov 22 at 12:08




did u specify any @model on top of view?
– er-shoaib
Nov 22 at 12:08












3 Answers
3






active

oldest

votes

















up vote
0
down vote













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




























    up vote
    0
    down vote













    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






























      up vote
      0
      down vote













      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',
        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








        up vote
        0
        down vote













        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

























          up vote
          0
          down vote













          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























            up vote
            0
            down vote










            up vote
            0
            down vote









            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 at 12:35









            Reha Bayar

            11




            11
























                up vote
                0
                down vote













                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



























                  up vote
                  0
                  down vote













                  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

























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    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 at 13:00

























                    answered Nov 22 at 12:45









                    KrishnaDhungana

                    1,67231626




                    1,67231626






















                        up vote
                        0
                        down vote













                        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



























                          up vote
                          0
                          down vote













                          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

























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            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 at 13:14

























                            answered Nov 22 at 13:03









                            Brendan Vogt

                            15.2k27118209




                            15.2k27118209






























                                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.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • 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

                                Contact image not getting when fetch all contact list from iPhone by CNContact

                                count number of partitions of a set with n elements into k subsets

                                A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks