1

I know a can save a single property to my db when knowing which propery I need to update, like this (the method SaveContent triggers trough a AJAX-post from the view):

public void SaveContent(string contentToUpdate)
{
    var page = Session.Load<CorePage>("CorePages/1");

    page.Title = contentToUpdate;
    RavenSession.SaveChanges();
}

But my problem is: the (string contentToUpdate)-parameter passed into my Save-method could be any property-value from my views @model-class because I use a WYSIWYG-editor om many places in the same view which means I never know which property that is passed in as string contentToUpdate. So I can't use the type of code a showed in my first exmple.

The string contentToUpdate could be any of these below:

public class CorePage
{
    public string WelcomeHeader { get; set; }
    public string WelcomeText { get; set; }
    public string LongText { get; set; }
    public string ShortDescription { get; set; }
}

So: How can I update a single-Property in var page = Session.Load<CorePage>("CorePages/1"); when the string contentToUpdate-parameter could be any? And which other parameters do a probably need to pass into the save-method to solve this (the propertyName)?

EDIT:

I don't need to create a new property, I need to find witch property to set the value to. So by passing in the propertyName as a parameter to my save-method I need to use that value(witch could be any of my properties in my class CorePage) to update that matching property. Basiclly, I never know witch of my properties from public class CorePage is being sent to public void SaveContent

Take a look at this below, and you propably know what I mean:

 public void SaveContent(string propertyName, string contentToUpdate)
 {
     //Philip Kendalls method used here
     SaveContent(page => page.propertyName, contentToUpdate);
 }
user3228992
  • 1,373
  • 1
  • 16
  • 31
  • possible duplicate of [Set object property using reflection](http://stackoverflow.com/questions/619767/set-object-property-using-reflection) as none of this is specific to RavenDB in any way; it's just about setting a property on an object. – Philip Kendall Sep 08 '14 at 10:02
  • Your web application needs to submit the name of the changed property. This has nothing to do with RavenDB. – Thomas Freudenberg Sep 08 '14 at 10:46

1 Answers1

1

If I've understood correctly, you want to update one of the four properties WelcomeHeader, WelcomeText, LongText or ShortDescription. You can do this with a little use of expression trees:

public void SaveContent(Expression<Func<CorePage, string>> propertyToSet, string contentToUpdate)
{
    var page = Session.Load<CorePage>("CorePages/1");
    var memberExpression = (MemberExpression) propertyToSet.Body;
    var property = (PropertyInfo) memberExpression.Member;
    property.SetValue(page, contentToSet);
    RavenSession.SaveChanges();
}

and then call this something like SaveContent(page => page.WelcomeHeader, "Welcome along!").

Philip Kendall
  • 4,304
  • 1
  • 23
  • 42
  • Thanks for the answer! Very helpful. But I also need to set the propertyName dynamiclly. Please look at my EDIT and I show you what I mean. – user3228992 Sep 08 '14 at 09:47
  • 1
    In that case, you just want [simple reflection](http://stackoverflow.com/a/619788/525010) and this is a duplicate of that question as it's nothing to do with RavenDB. – Philip Kendall Sep 08 '14 at 10:01
  • As I formulated my question first, your solution answers that! And for my EDIT-part, the reference to [link](http://stackoverflow.com/questions/619767/set-object-property-using-reflection/619788#619788) was also very helpful. – user3228992 Sep 08 '14 at 10:25