My data is coming from a SQL server database by way for the Entity Frame work,
I am getting the following error:
<a href="@Model.POSIT_JOB_URL">Click Here"</a>
'IEnumerable<JobsIndex_ViewModel_Class>' does not contain a definition for 'POSIT_JOB_URL' and no accessible extension method 'POSIT_JOB_URL' accepting a first argument of type 'IEnumerable<JobsIndex_ViewModel_Class>' could be found (are you missing a using directive or an assembly reference?)
I don't think this error is correct because there is a POSIT_JOB_URL in the view Model
THis is the JobsIndex_ViewModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace JobsOpeningBackendSite.VIEW_MODELS
{
public class JobsIndex_ViewModel_Class
{
[Key]
[DisplayName("Position Number")]
public short POSIT_NUMBER { get; set; }
[DisplayName("Job Posting Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public System.DateTime POSIT_POSTING_DATE { get; set; }
[DisplayName("Job Closing Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public System.DateTime POSIT_CLOSING_DATE { get; set; }
[DisplayName("Position Description")]
public string POSIT_DESCRIPTION { get; set; }
[DisplayName("Click Here")]
public string POSIT_JOB_URL { get; set; }
public int PRID { get; set; }
public Nullable<bool> POSIT_POST_STATUS { get; set; }
}
//See: https://stackoverflow.com/questions/33044606/pass-ienumerable-viewmodel-to-view
//See: https://stackoverflow.com/questions/21694691/the-model-item-passed-into-the-dictionary-is-of-type-system-collections-generic
//See: http://techfunda.com/howto/262/list-data-using-viewmodel
public class JobsIenumViewModel
{
public IEnumerable<JobsIenumViewModel> jobs_ViewModel_Class { get; set; }
}
}
So I am not sure why it is stating that the item is not part of the view model.
This is the Index Item in the MVC Controller:
// GET: JOBS_POST_TBL
public ActionResult Index()
{
//var ListOfJobs = (from JobPost in db.JOBS_POST_TBL select new { JobPost.POSIT_CLOSING_DATE, JobPost.POSIT_POSTING_DATE, JobPost.POSIT_NUMBER, JobPost.POSIT_DESCRIPTION, JobPost.POSIT_JOB_URL }).ToList();
//Using View Model vs the Table
var ListOfJobs = db.JOBS_POST_TBL.Include("JOBS_POST_TBL").Select(j => new JobsIndex_ViewModel_Class { POSIT_CLOSING_DATE = j.POSIT_CLOSING_DATE, POSIT_POSTING_DATE = j.POSIT_POSTING_DATE, POSIT_NUMBER = j.POSIT_NUMBER, POSIT_DESCRIPTION = j.POSIT_DESCRIPTION, POSIT_JOB_URL = j.POSIT_JOB_URL });
return View(ListOfJobs);
//return View(db.JOBS_POST_TBL.ToList());
}
You added to your original post and did not have this information when I initially responded. The View is building a list using a foreach() block. Use the "item" which points to the current array element in the loop.
@foreach (var item in Model)
{
<a href="@item.POSIT_JOB_URL">Click Here</a>
foreach loops are covered in the C# programming guide.
Its too bad that there is not something that stated that something like this would work inside of a razor.
Yeah, this information is openly published. The C# programming guide shared above. Even the wizard you like to use will render a foreach loop in a View when the List option is selected.
Your are struggling with fundamentals. A URL is just a string and Views let you insert whatever string value you like right in the View's HTML using Razor syntax. Razor syntax is C# that starts with an @.
Anyway, for a test, I let the wizard create a View and guess what, it generated a foreach loop. It also created an HTML table which might not be the presentation you're looking for but if you know HTML then you can change it.
All you had to do was copy the highlight item above to the link tag and add an "@" in front of the C# code. It's like saying, "Hey view put this C# value right here in the HTML.".
But, if you do not understand a foreach loop, Razor syntax, and HTML, you will have a very hard time building a View. You should learn this stuff rather than surfing the web for a "copy and paste" solution.
I do understand the data structure I Just did not know it could be used to display the URL as a URL. The for loop was not my issue. I was simply looking for away to display the url that was in the database as a clickable link. That an nothing more.
Not sure why you are on about.. incorrectly that I am having some issue with loops... I went looking for how you convert data from the data into a clickable url.
The research lead me down an incorrect road. I hope that help you understand better as I was not asking about the looping structure but how you do things in razor.
Member
148 Points
677 Posts
Is there a simple way to have a URL that is stored in a sql database function as a clickable link...
Jan 23, 2020 04:21 PM|AppDev01|LINK
I seem to find myself stuff in an interesting situation here.
I have some web site urls that is stored in a sql database field .
Im my index view I am trying to have that URL make a clickable link to the site listed in the database.
I need to URL to show up a Click Here and not show as the actual URL.. is this possible in a razor index view?
Or is it not possible to render a Clickable URL from a value stored in a sql server data base
This is my Index view :
I tried this but I am Getting errors:
Can some one point me to and example or some information oh how this can be done.
I have read somethings on use the powerful viewbag feature but I don't need to have the URL display the full
URL. I need it to simple state as a clickable like Click Here.
I have also Tried :
I am not 100% sure what t search for terminology wise.
Thanks all.
All-Star
53001 Points
23587 Posts
Re: Is there a simple way to have a URL that is stored in a sql database function as a clickable...
Jan 23, 2020 04:43 PM|mgebhard|LINK
Your syntax is incorrect. It is a capital M in model.
Here's an example.
If the link is external you need to add http or https.
Edit: your action link syntax is also incorrect. Please read the Html.ActionLink() reference documentation.
<div> <a href="@Model.POSIT_JOB_URL">Click Here</a> @Html.ActionLink("Click Here", "Home", "About") </div>
Member
148 Points
677 Posts
Re: Is there a simple way to have a URL that is stored in a sql database function as a clickable...
Jan 23, 2020 05:22 PM|AppDev01|LINK
Hello and Thanks,
My data is coming from a SQL server database by way for the Entity Frame work,
I am getting the following error:
I don't think this error is correct because there is a POSIT_JOB_URL in the view Model
THis is the JobsIndex_ViewModel
So I am not sure why it is stating that the item is not part of the view model.
This is the Index Item in the MVC Controller:
All-Star
53001 Points
23587 Posts
Re: Is there a simple way to have a URL that is stored in a sql database function as a clickable...
Jan 23, 2020 06:05 PM|mgebhard|LINK
You added to your original post and did not have this information when I initially responded. The View is building a list using a foreach() block. Use the "item" which points to the current array element in the loop.
@foreach (var item in Model) {
<a href="@item.POSIT_JOB_URL">Click Here</a>
foreach loops are covered in the C# programming guide.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/using-foreach-with-arrays
Member
148 Points
677 Posts
Re: Is there a simple way to have a URL that is stored in a sql database function as a clickable...
Jan 23, 2020 06:49 PM|AppDev01|LINK
Wow that actually worked.
Its too bad that there is not something that stated that something like this would work inside of a razor.
All of the examples that I found did not state using the item keyword.
No I Know this is possible it really opens up some options.
Many thanks! This worked great!
All-Star
53001 Points
23587 Posts
Re: Is there a simple way to have a URL that is stored in a sql database function as a clickable...
Jan 23, 2020 06:55 PM|mgebhard|LINK
Yeah, this information is openly published. The C# programming guide shared above. Even the wizard you like to use will render a foreach loop in a View when the List option is selected.
Member
148 Points
677 Posts
Re: Is there a simple way to have a URL that is stored in a sql database function as a clickable...
Jan 23, 2020 08:45 PM|AppDev01|LINK
Well I have a foreach loop and that was in the early question...
I showed that here : https://forums.asp.net/p/2163420/6291725.aspx?p=True&t=637151968921861691
What I did not know was how one would go about displaying that as a url. So in searching for a solution for a couple of days before asking...
I found other examples that sent be down the wrong path... Programming is difficult in those ways. So may differing answers.
Who would have know you could do that in such a simple way.
Thanks
All-Star
53001 Points
23587 Posts
Re: Is there a simple way to have a URL that is stored in a sql database function as a clickable...
Jan 23, 2020 09:23 PM|mgebhard|LINK
Your are struggling with fundamentals. A URL is just a string and Views let you insert whatever string value you like right in the View's HTML using Razor syntax. Razor syntax is C# that starts with an @.
Anyway, for a test, I let the wizard create a View and guess what, it generated a foreach loop. It also created an HTML table which might not be the presentation you're looking for but if you know HTML then you can change it.
All you had to do was copy the highlight item above to the link tag and add an "@" in front of the C# code. It's like saying, "Hey view put this C# value right here in the HTML.".
<a href="@item.POSIT_JOB_URL">@item.POSIT_JOB_URL</a>
But, if you do not understand a foreach loop, Razor syntax, and HTML, you will have a very hard time building a View. You should learn this stuff rather than surfing the web for a "copy and paste" solution.
Member
148 Points
677 Posts
Re: Is there a simple way to have a URL that is stored in a sql database function as a clickable...
Jan 23, 2020 09:30 PM|AppDev01|LINK
I do understand the data structure I Just did not know it could be used to display the URL as a URL. The for loop was not my issue. I was simply looking for away to display the url that was in the database as a clickable link. That an nothing more.
Not sure why you are on about.. incorrectly that I am having some issue with loops... I went looking for how you convert data from the data into a clickable url.
The research lead me down an incorrect road. I hope that help you understand better as I was not asking about the looping structure but how you do things in razor.