0

this is my controller code:

    var myquery = mycontext.Websites.Select(x => new {x.pagetype,x.website1,x.creationdate,x.expirationdate,x.domainregistrar,x.area,x.pin
                    ,x.city, age = DateTime.Now.Subtract(x.expirationdate ?? DateTime.Today)
                });

return View(myquery);

and this is my view code:

@foreach (var item in Model)
    {
        <tr>
            <td>
                <span>@item.pagetype </span>

</td>

</tr>

}

how ever its giving me error: object does not contain definition of pagetype while traversing through foreach loop?

i wonder why?

EDIT:

I tried changing the code as below:

Controller:

   CRMDataContext mycontext = new CRMDataContext();
                var myquery = mycontext.Websites.Select(x => new WebsiteViewModel
                {
                    pagetype = x.pagetype,
                    site = x.site,
                    creationdate = x.creationdate ?? DateTime.Today,
                    expirationdate = x.expirationdate ?? DateTime.Today,
                    domain_registrar = x.domainregistrar,
                    Area = x.area,
                    pin = x.pin,
                    City = x.city,
                   difference = DateTime.Now.Subtract(x.expirationdate ?? DateTime.Today).ToString()
                }).ToList();

     return View(myquery);

but i'm getting exception:

Operation could destablize operation at runtime. Make sure your application is not loading two conflicting version of class library

all i can think..is linq generated class for Website table and my Websiteview model is conflicting . but i can not understand why?

1
  • 2
    You can't return anonymous type from Controller to View. You have to create a model class and return it instead. Commented Mar 29, 2013 at 15:01

2 Answers 2

2

To elaborate on the comment by MarcinJuraszek above, you need to create a view model class to hold these properties.

ViewModel class

public class MyViewModel{
   public string Pagetype {get;set;}
   public string Website1 {get;set;}
   public DateTime Creationdate {get;set;}
   public DateTime Expirationdate {get;set;}
   public string Domainregistrar {get;set;}
   public string Area {get;set;}
   public string Pin {get;set;}
   public string City {get;set;}
   public int Age {get;set;}
}

Now, in your query, select into the ViewModel class

var model = mycontext.Websites.Select(x => new MyViewModel{
                  PageType = x.pagetype
                  WebSite1 = x.website1
                  CreationDate = x.creationdate
                  ExpirationDate = x.expirationdate
                  DomainRegistrar = x.domainregistrar
                  Area = x.area
                  Pin = x.pin
                  City = x.city,
                  Age =DateTime.Now.Subtract(x.expirationdate ?? DateTime.Today)
               }).ToList();

return View(model);

And now, you need to strongly type your view and you will be good to go!

@model IEnumerable<MyViewModel>

@foreach (var item in Model)
    {
        <tr>
            <td>
                <span>@item.Pagetype </span>

</td>

</tr>

}
Sign up to request clarification or add additional context in comments.

1 Comment

it's giving me another error . please check my edit in my post.
0

After 1 whole day i could solve it out like below:

public WebSiteViewModel
{
     string PageType { get; set;}
     string WebSite { get; set;}
     DateTime CreationDate { get; set;}
     DateTime ExpirationDate { get; set;}
     string DomainRegistrar { get; set;}
     string Area { get; set;}
     string Pin  { get; set;}
     string City { get; set;}
     DateTime Age {get; set;}

     //Constructor
     public WebSiteViewModel(YourWebSiteEntity w)
     {
             PageType = w.pagetype;
             WebSite = w.website1;
             CreationDate = w.creationdate;
             ExpirationDate = x.expirationdate;
             DomainRegistrar = x.domainregistrar;
             Area = x.area;
             Pin = x.pin;
             City = x.city;
             Age = DateTime.Now.Subtract(x.expirationdate ?? DateTime.Today);
     }
}

controller:

public ActionResult Index()
        {
            CRMDataContext mycontext = new CRMDataContext();


            var myquery = mycontext.Websites.Select(x => new WebsiteViewModel(x.pagetype,
               x.site, x.creationdate ?? DateTime.Today, x.expirationdate ?? DateTime.Today, x.domainregistrar, x.pin, x.area, x.city, "Abc")).ToList();

            return View(myquery);
        }

View:

@model IEnumerable<ImportFromExcel.Model.WebsiteViewModel>
@{
    ViewBag.Title = "Index";
}
<h2>
    Index</h2>
<table>
    <tr>
        <th>
            Page type
        </th>
        <th>
            Website
        </th>
        <th>
            Creation date
        </th>
        <th>
            Expiration Date
        </th>
        <th>
        difference
        </th>
        <th>
            Domain Registrar
        </th>
        <th>
            Pin
        </th>
        <th>
            Area
        </th>
        <th>
            City
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                <span>@item.pagetype </span>
            </td>
            <td>
                <span>@item.site </span>
            </td>
..so on
        </tr>

    }
</table>

Hope some body else could find it useful as well. Problem was i was not using WebsiteViewModel Constructor to initiliaze model object . this solved my problem atlast. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.