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.
c# html asp.net-core
|
show 13 more comments
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.
c# html asp.net-core
try to setViewData["ImagePath"] = pigeonDetails;
just before thereturn 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
|
show 13 more comments
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.
c# html asp.net-core
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
c# html asp.net-core
edited Nov 22 at 12:36
Matt
176111
176111
asked Nov 22 at 11:51
stefan van schoor
75
75
try to setViewData["ImagePath"] = pigeonDetails;
just before thereturn 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
|
show 13 more comments
try to setViewData["ImagePath"] = pigeonDetails;
just before thereturn 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
|
show 13 more comments
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 ;)
add a comment |
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" />
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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 ;)
add a comment |
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 ;)
add a comment |
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 ;)
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 ;)
answered Nov 22 at 12:35
Reha Bayar
11
11
add a comment |
add a comment |
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" />
add a comment |
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" />
add a comment |
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" />
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" />
edited Nov 22 at 13:00
answered Nov 22 at 12:45
KrishnaDhungana
1,67231626
1,67231626
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 22 at 13:14
answered Nov 22 at 13:03
Brendan Vogt
15.2k27118209
15.2k27118209
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
try to set
ViewData["ImagePath"] = pigeonDetails;
just before thereturn 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