0

This is the method in my Model :

 public IList<Customer> GetProfileCustomer(int id)
    {
        var list_customer = from c in DataContext.Customers
                            where c.ID == id
                            select c;
        return list_customer.ToList();
    }

And this is what I did in my controller :

   public ActionResult ShowProfile()
    {
        List<CustomerModels> cus = new List<CustomerModels>();

        return View();
    }

I created the object cus to call the method GetProfileCustomer() in the model, but I cannot do that. When I write : cus.GetProfileCustomer, It is error.

4
  • 1
    can you provide the error details? Commented Dec 17, 2011 at 3:50
  • you need to call GetProfileCustomer on model object not on the list object Commented Dec 17, 2011 at 3:55
  • Can you tell me clearly, what should I do that? Thanks. Commented Dec 17, 2011 at 3:57
  • i guess your model name is Customer...so you need to use an object of Customer model. such as customer.GetProfileCustomer(id)....but my advise will be to move GetProfileCustomer method in controller Commented Dec 17, 2011 at 4:01

3 Answers 3

1

Your major issue is the line:

List<CustomerModels> cus = new List<CustomerModels>();

That is not creating an instance of CustomerModels, so you can't call that method on it. You would have to do something like:

public ActionResult ShowProfile()
{
    cus = new CustomerModels();
    var data = cus.GetProfileCustomer(123);
    return View(data);
}

However, in the MVC sense, I don't think loading data from your Model is really the right approach. Typically the Controller has some reference to something else that loads and saves data. The Model is usually just a class with properties to hold the data.

I would look at the "NerdDinner" sample project. For example, in these files:

Note that DinnersController holds a reference to a dinner repository, which is the thing that queries the database.

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

Comments

0

AVD is right, You will need to first instantiate the class that houses the GetProfileCustomer method i.e:

public ActionResult ShowProfile(int id)
{
    var model = new WhateverTheClassNameIs().GetProfileCustomer(id);

    return View(model);
} 

Comments

0

May be you have to create instance of CustomerModels instead of List<CustomerModels>.

public ActionResult ShowProfile()
    {
        CustomerModels cus=new CustomerModels(); 
        var list=cus.GetProfileCustomer(1); // 1 is value of ID
        return View(list);
    }

You can iterate Model in view (Razor)

  <div>
        @foreach(var cust in Model){
            <br /><b>ID :</b> @cust.CustomerID
             }
    </div>

ASPX markup

<div>
      <% foreach (var cust in Model)
         { %>

         <br />ID : <%:cust.CustomerID %>

      <% } %>
    </div>

2 Comments

Thanks. But could you tell me, how can I call this controller in my view?
@socheata - Take a look at post. You have to pass "list" via View(list) argument.

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.