Model not being found

M J 661 Reputation points
2021-07-01T16:56:46.16+00:00

I screwed up and named an area Applicant and I have a model Applicant.

I went ahead and deleted the area Applicant and created a new one named Public in place of the old one.

Now it is not finding the model Applicant when I try to reference it unless I use Models.Applicant

Here is the model

namespace Greenbridge.Models
{
    public class Applicant
    {
        public string ApplicationId { get; set; }
        public string UserId { get; set; }
        public string Gender { get; set; }
        public string Email { get; set; }
        public string PhoneNumber { get; set; }
        public int JobId { get; set; }
        public int LocationId { get; set; }
        public DateTime DateApplied { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string JobTitle { get; set; }
        public string City { get; set; }

    }
}

Not a big deal but I have a page that lists the applicants and have this at the top of the page

 @model IEnumerable<Greenbridge.Models.Applicant>

and then when I try to iterate through the applicants and use

@foreach (var item in Model)
{
 string PersonName = Model.LastName + ", " + Model.FirstName;
 <tr>
 <td> </td>
 <td> </td>
 </tr>
}

it shows a red squiggly line under the LastName and says the following

'IEnumerable<Applicant>' does not contain a definition for 'LastName' and no accessible extension method 'LastName' accepting a first argument of type 'IEnumerable<Applicant>' could be found (are you missing a using directive or an assessmbly reference?)

And the same for the FirstName

'IEnumerable<Applicant>' does not contain a definition for 'FirstName' and no accessible extension method 'FirstName' accepting a first argument of type 'IEnumerable<Applicant>' could be found (are you missing a using directive or an assessmbly reference?)

How do I correct? It builds ok with no errors shown during the build but it does show those red squiggly lines.

As I said I have deleted the Applicant Area already.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,506 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yijing Sun-MSFT 7,086 Reputation points
    2021-07-02T04:27:02.93+00:00

    Hi @M J ,
    You need to use item.LastName and item.FirstName. And you need to return your data in the view.
    Just like this:

    @foreach (var item in Model)  
    {  
        string PersonName = item.LastName + ", " + item.FirstName;  
        <tr>  
            <td> </td>  
            <td> </td>  
        </tr>  
    }  
    public ActionResult Index()  
            {  
                var list = new List<Applicant>()  
                {  
                    new Applicant() { ApplicationId = "1",UserId="AA",Gender="AA",Email="AA",PhoneNumber="AA",JobId=1,LocationId=1,   
                                                FirstName="AA",LastName="AA",JobTitle="AA",City="AA"},  
                    new Applicant() {ApplicationId = "2",UserId="bb",Gender="bb",Email="bb",PhoneNumber="bb",JobId=1,LocationId=1,   
                                               FirstName="bb",LastName="bb",JobTitle="bb",City="bb" },  
                    new Applicant() {ApplicationId = "3",UserId="c",Gender="cc",Email="cc",PhoneNumber="cc",JobId=1,LocationId=1,   
                                               FirstName="cc",LastName="cc",JobTitle="cc",City="cc" },  
                };  
                return View(list);  
            }  
    

    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.