LinqConect MVC example needed

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
Sulphy
Posts: 34
Joined: Wed 12 Sep 2012 18:07

LinqConect MVC example needed

Post by Sulphy » Thu 27 Dec 2012 15:12

Hi all,

I'm new to MVC 3 and the Devart LinqConnect product and hit a little issue, namely due to my lack of understanding I think.

What I'm trying to achieve is a working example that I can build from to help with the learning curve.

In this example I've set myself a goal where:

Using VS 2010 and latest version of Devart LinqConnect Pro.
1) create a function in a model to pull back a list of names from a table.
2) pass the list from the model to the controller and from the controller to a view

Those of you reading this that are seasoned programmers might already be asking why am I taking the above approach. If my understanding is correct what I'm doing is putting my business logic in a new model under the models folder to conform with the mantra around seperation of concerns. So in my mind the correct process is to have the bulk of my web application logic and functions in my model folder, the controllers to direct my user and the views to render out my data (hopefully that is the right approach).


So I have a model called ContactModel.cs under the Models folders with the following code:

Code: Select all

public class Contacts
    {
       public Contacts()
        {
            MyDBDataContext db = new MyDBDataContext ();

            Table<TBLCONTACTNAME> TBLCONTACTNAMEs = db.GetTable<TBLCONTACTNAME>();
             var MyContacts =
                from c in TBLCONTACTNAMEs 
                select c.FirstName;
        }
    }
Although not listed above I used a foreach statement with a breakpoint set over the loop to ensure that data was successfully being pulled back, which it was.

And this is where I've got stuck. My next goal is to get the HomeController.cs under the controllers folder to use this function to get a list back and pass it to a view to render the data. Something like the below in the HomeController:

Code: Select all


 public ActionResult ShowContacts()
        {
            Contacts s = new Contacts();
            return View("ShowContacts", s);
        }
And finally in the view called ShowContacts I would have something like:

Code: Select all


<h2>Show Contacts</h2>

<fieldset>
    <legend>Contact List</legend>
 @foreach (var item in Model)
 {
    @Html.Encode(item.FirstName)
 }
</fieldset>
I believe that in the first code example for ContactModel.cs I need

Code: Select all

 return MyContacts.ToList(); 
however I get an error along the lines of: Cannot implicitly convert type ....

I'm pretty sure the answer is easy, and I just need a working example / guidance that I can follow which should get me moving again. I can get the data OK, it's now just a case of being able to use it in a function that I can pass to a view to output on screen.

Many thanks in advance to any and all replies :o)

StanislavK
Devart Team
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Re: LinqConect MVC example needed

Post by StanislavK » Fri 28 Dec 2012 17:38

From what is written, you are passing a Contacts object to (which is not a collection) the view, and then trying to enumerate this object.

The correct way can be to, e.g., expose the name list via a property:

Code: Select all

public class Contacts {

  public IList<string> FirstNames { get; private set; }

  public Contacts() {
    ...
    FirstNames = 
      (from c in TBLCONTACTNAMEs 
      select c.FirstName)
      .ToList();
  }
}
Then you can access this property in the view:

Code: Select all

  @foreach (var item in Model.FirstNames)
  {
    @Html.Encode(item.FirstName)
  }
Also, I'm sending you an ASP.NET MVC sample with the repository and inversion of control implementation. Please check that your mail filter is not blocking the letter.

Sulphy
Posts: 34
Joined: Wed 12 Sep 2012 18:07

Re: LinqConect MVC example needed

Post by Sulphy » Fri 28 Dec 2012 19:35

Hi StanislavK,

Thank you so much for replying! Your email came through OK and i've spent some time looking at the example below and the email attachement.

I've followed the example below and seem to have hit another little issue.

Code: Select all

@foreach (var item in Model.FirstNames)
  {
    @Html.Encode(item.FirstName)
  }
When I type item. I believe that the intellisense should list FirstName in the list, but it doesn't. If I type in item.FirstName manually I get the following error when I run the app:

Compiler Error Message: CS1061: 'string' does not contain a definition for 'FirstName' and no extension method 'FirstName' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

I put a breakpoint on the model and an entry appears for FirstNames which has all the entries from my DB listed OK.

The intellisense for var item in Model. works ok as 'FirstNames' appears in the list. So I don't understand at this point as to why nothing appears for item. - I assume any fields from my collection should be listed?

Many thanks again!

P :D

StanislavK
Devart Team
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Re: LinqConect MVC example needed

Post by StanislavK » Sat 29 Dec 2012 13:40

The FirstNames collection is a list of strings, hence 'item' is just a string.

If you want to pass, e.g., contact objects to the view, you should expose them instead:

Code: Select all

public class Contacts {
  public IList<TBLCONTACTNAME> All { get; private set; }

  public Contacts() {
    ...
    All = TBLCONTACTNAMEs.ToList();
  }
}
In this case, elements of the Contacts.All collection are 'full' contacts with the FirstName property.

Sulphy
Posts: 34
Joined: Wed 12 Sep 2012 18:07

Re: LinqConect MVC example needed

Post by Sulphy » Sat 29 Dec 2012 17:00

Hi again,

That did the trick!! Thank you so much! I've been mulling this over for days and now I can start making progress again.


Kind regards,

P :D

Post Reply