0

I want to retrieve some files using the contact id

here's my code.

    public string Edit(int id)
    {
        var GetContacts = from c in db.Contacts where c.ContactID == id
                        select c;

        return GetContacts.ToString();

    }

when I go to contacts/edit/1 for example. it displays this.

System.Data.Objects.ObjectQuery`1[ContactsMVC.Models.Contact]

what is wrong with my code? is it in the query. I actually want to get the name, mobile, and email of the user.

I am new to asp.net C# mvc 2 so bear with me.

thanks in advance.

4
  • var GetContacts returns IEnumerable<Contact> so assuming you want that, then your method should be public IEnumerable<Contact>Edit(int id) (all you currently doing is returning the ToString() value of IEnumerable<Contact>) Commented Sep 22, 2015 at 2:25
  • @StephenMuecke yes since I don't know how to get the name, mobile and etc. from the object.. Commented Sep 22, 2015 at 2:29
  • Your returning a collection of contacts (not one) so if you want the first one in the collection it needs to be var contact = (from c in db.Contacts where c.ContactID == id select c).FirstOrDefault(); and then you change the method to public Contact Edit(int id) (and access the properties of the model) Commented Sep 22, 2015 at 2:34
  • But since you seem to be wanting to navigate to a controller method, Then it needs to be public ActionResult Edit(int ID) { var contact = ...as above..; return View(contact); } Commented Sep 22, 2015 at 2:36

1 Answer 1

1

Change your return type

public IEnumerable<Contacts> Edit(int id)
    {
        var GetContacts = from c in db.Contacts where c.ContactID == id
                        select c;

        return GetContacts.ToList();

    }

OR you can return as Json Object

 public ActionResult Edit(int id)
        {
            var GetContacts = from c in db.Contacts where c.ContactID == id
                            select c;

            return Json(GetContacts.ToList(), JsonRequestBehavior.AllowGet);
        }

Client Side - (for json one):

$.get("yourController/Edit",{id:yourid}, function(data){
   alert(data.Name); // For example
})
Sign up to request clarification or add additional context in comments.

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.