Is it possible to write a query and select an item where its propertyName
equals a string?
I want to pass in a string to a method, where my string holds the value of a propertyName
. And then use it in a query to select my property that equals that string. Is this possible?
I know I'm not writing any code here, just asking if this is possible to do.
EDIT: Ok this as my solution a come up wth so far, and I know, it's far from a good solution. But I will put here and if you can give me a better solution, feel free to do it. Any help is a appreciated!
This is one of the Raven-document I could work with:
{
"WelcomeHeader": "header",
"WelcomeText": "some text",
"Template": {
"Id": "RouteConfigTemplates/1",
"Name": "Index",
"Controller": "Home",
"Action": "Index",
"View": "Index",
"ContentPageType": "Core_StudioL.Models.IndexCorePage, Core_StudioL"
},
"Url": "/"
"Title": "Start"
}
Im my view a have a couple of WYSIWYG-editors, and each time I push the save button on one of my editors i sending the parameters to this mvc-controller-method below:
string model
: is my whole model in json-format as string.
string activeId
: is the id of the passed WYSIWYG-editors div (activeId = The sent propertys propertyName)
string contentToUpdate
: is the value I want to update
[ValidateInput(false)]
public JsonResult SaveContent(string model, string activeId, string contentToUpdate)
{
//parse model to json
var modelAsJson = JObject.Parse(model);
//if the property-name == item.key, replace it
foreach (var item in modelAsJson)
{
if (item.Key == activeId)
{
item.Value.Replace(contentToUpdate);
}
}
//find the right contentPageType
string cType = modelAsJson["Template"]["ContentPageType"].ToString();
//convert back to string
string newModel = modelAsJson.ToString(Formatting.None);
//here I need to have many if-statments (not an optimal solution) to pick out the right contentPageType, here is a example of one of them.
//The problem here
if (cType.Equals("Core_StudioL.Models.IndexCorePage, Core_StudioL"))
{
var jsonSerializer = new JavaScriptSerializer();
var modelAsCorrectModel = jsonSerializer.Deserialize<IndexCorePage>(newModel);
//Here I'm having problem to update a single value, because the values sent in to the controller could be any..
//So my ugly solution here is to delete the whole document from raven, and then save a new one with the updated values. If a just could only save the `string contentToUpdate`
//
var page = RavenSession.Load<CorePage>(modelAsCorrectModel.Id);
RavenSession.Delete(page);
RavenSession.SaveChanges();
RavenSession.Store(modelAsCorrectModel);
RavenSession.SaveChanges();
var modelToReturn = new JavaScriptSerializer().Serialize(modelAsCorrectModel);
return Json(modelToReturn);
}
return null;
}
by doing it this way, my model in the razor-view never gets updated before I reload the page. That means: If a save on of my WYSIWYG-editors, it's content gets updated to ravendb. But if I save another one without refreshing the page after I saved the first one. Then just that another one gets updated. Because the model sent in to the save-method is still the first model sent in to the view. So: the first one saved, then gets overwritten by the original Model, because it still holds the original models values!