Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Is correct way of updating ContentItem in Orchard?

I am trying to update a specific part with following code:

var nationalPart = _contentManager.Get<NationalPart>(Id);      
nationalPart.Name = part.Name;

i have already done this.but now it is not working.even i call contentManager.Flush() explicitly, but still no luck. can anyone help me with this? thanks in advance.

EDIT: i found where my problem originates from! when i make a JSON request to update a contentPart.the in memory version is updating but it is not reflecting the result to db.now we are one step closer to answer.but what is preventing the session from updating the db?

EDIT2:

this is code for my handler :

        public NationalPartHandler(IRepository<NationalPartRecord> repository)
        {
            Filters.Add(StorageFilter.For(repository));
        }

this is code for Controller Action:

    [GridAction(EnableCustomBinding = true)]
    public JsonResult UpdateNational(NationalViewModel Model, GridCommand Command)
    {
         if (!ModelState.IsValid)
            return new JsonResult { Data = "error" };
        var NationalPart = _contentmanager.Get(Model.Id).As<NationalPart>();
        NationalPart.Record = new NationalPartRecord();
        NationalPart.Record.MapFrom<NationalPartRecord, NationalViewModel>(Model);
        _soccerService.UpdateNational(NationalPart);         
        return NationalsListJson(Command);
    }

and finally code for service:

public void UpdateNational(NationalPart part)
{
    var nationalPart = _contentManager.Get<NationalPart>(part.Id);      
    nationalPart.Name = part.Name;
    _contentManager.Flush();
}
like image 343
Behnam Esmaili Avatar asked Oct 15 '12 14:10

Behnam Esmaili


1 Answers

Remove the line

NationalPart.Record = new NationalPartRecord();

The underlying record is automatically generated and is proxied so NHibernate can save the data to the database whenever you do any change to it. You don't want to replace that ever, otherwise no data will get saved.

UpdateNational is redundant and you can remove it - you already have part.Name (which if I understand correctly, should map to part.Record.Name) set during call to record.MapFrom.

So the correct version would look like this

[GridAction(EnableCustomBinding = true)]
public JsonResult UpdateNational(NationalViewModel Model, GridCommand Command)
{
     if (!ModelState.IsValid)
        return new JsonResult { Data = "error" };
    var part = _contentmanager.Get(Model.Id).As<NationalPart>();
    part.Record.MapFrom<NationalPartRecord, NationalViewModel>(Model);        
    return NationalsListJson(Command);
}

given your NationalPart looks similar to:

public NationalPart : ContentPart<NationalPartRecord>
{
    public string Name {
        get { return Record.Name; }
        set { Record.Name = value; }
    }
}

Summarizing - if you want to store some data in Orchard, all you need to do is to set a value of some property of a record, nothing more. Data will get persisted at the end of a request (or earlier, if you call IContentManager.Flush()).

like image 171
Piotr Szmyd Avatar answered Nov 05 '22 05:11

Piotr Szmyd