aruni
aruni

Reputation: 2752

How to assign partial view data to model in c# MVC?

I'm using MVC C# and entity frame work.

So in my partial view I want to change data.

This is my controller

public ActionResult BusinessDetails(){               
    int bsId = bskey ?? default(int);
    var BusinessQuery = GetBusinessById(bsId);    
    var PeopleQuery = BusinessQuery.t_PEOPLE
                        .Where(p => p.PP_IS_CONTACT == true).AsEnumerable();

    var peopleCollection = new EntityCollection<t_PEOPLE>();

    foreach (var m in PeopleQuery)
    {
        //This is problem.-----------
        peopleCollection.Add(m);
    }    //--------------

    BusinessQuery.t_PEOPLE = peopleCollection;    
    return PartialView("_EditBusiness", BusinessQuery);
    }                    

    return RedirectToAction("Index");              
}


public t_BUSINESS GetBusinessById(int id){
    if (id == 0)
        return null;

    var query = from b in db.t_BUSINESS
                where b.BS_KEY == id
                select b;
    var Business = query.FirstOrDefault();
    return Business;
}

So how can I assign the PeopleQuery to my BusinessQuery.t_PEOPLE.

Ther is error and says

The object could not be added to the EntityCollection or EntityReference. An object that is attached to an ObjectContext cannot be added to an EntityCollection or EntityReference that is not associated with a source object

Upvotes: 0

Views: 372

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039318

You could directly query the records you are looking for:

public ActionResult BusinessDetails()
{               
    int bsId = bskey ?? default(int);
    var business = GetContactBusinessById(bsId);    
    return PartialView("_EditBusiness", business);
}

public t_BUSINESS GetContactBusinessById(int id)
{
    if (id == 0)
    {
        return null;
    }

    var query = 
        from b in db.t_BUSINESS
        from p in b.t_PEOPLE
        where b.BS_KEY == id && p.PP_IS_CONTACT
        select b;

    return query.FirstOrDefault();
}

Upvotes: 2

Satpal
Satpal

Reputation: 133433

try this

var PeopleQuery = BusinessQuery.t_PEOPLE.Where(p => p.PP_IS_CONTACT == true).ToList();

Upvotes: 1

Related Questions