GlassMapper does not map and Image property when using SitecoreContext.Map(result); - glass-mapper

We are using a repository to get Sitecore items from Solr and then map them to a Sitecore object using SitecoreContext.Map. Everything gets data except the Image property which is null.
The model is generate from TDS and the property is:
[SitecoreField(I_Base_Content_ItemConstants.ImageFieldName)]
Glass.Mapper.Sc.Fields.Image Image {get; set;}
We are doing nothing custom.
Getting data from solr
Mapping using SitecoreContext.Map
Image property on our generated TDS class is Null
Thanks
Charlie

If you are using a TDS generated class make sure that you have this property on it:
[TypeConverter(typeof(IndexFieldItemUriValueConverter))]
[XmlIgnore]
[IndexField("_uniqueid")]
public virtual ItemUri Uri { get; set; }
Glass needs this to link the Solr item to the Sitecore item.

Mike it turned out to be because we were calling the map method and found the the id was property was empty. We fixed that by getting the value from sitecore and it worked :). Thanks.

Related

ASP.NET MVC 3 Client side method to show or hide a field based on the value of a related field using attributes

For my model I require an attribute somewhat like the following:
[Required (ErrorMessage = "Please select yes or no")]
public bool IsAlien {get; set; }
[ConditionalDisplayIfTrue ("IsAlien ")]
public string NameOfGalaxy {get; set;}
And then show or hide the text box client side if IsAlien is selected.
Is's very easy to hardwire it in jQuery/Jvascript but I would like to do this properly and have no idea where to start.
By properly I mean to add custom attributes and custom JavaScript to hook up with unobtrusive JavaScript validation.
Thanks in advance.
I think doing this via jQuery/Javascript is the proper way to do it. Any C# library you use is going to generate the requisite javascript on the server and send it back to the browser for you. I don't know any library that supports this right now, but for something so simple would prefer to have control over my javascript than use somebody elses.

How to create and implement a custom datatype in mvc3 razor

I googled the problem too many times. But I couldnt find any solution to do this.
I want to create a custom DataType with a default EditorTemplate and DisplayTemplate for use in mvc3 razor.
Model
[DataType("MyCustomDataType")]
public MyType Property { get; set; }
// I mean by MyType any type of data: string, int, datetime and so on
View - Razor
#Html.DisplayFor(m => m.Property)
//or
#Html.EditorFor(m => m.Property)
Actually I want to create a reuseable datatype with default editor-template and display-template.
Do you have any suggestion please? Can you give me a useful link or if you can, a simple example please? Thanks in advance.
this is an excellent tutorial for custom editor/display template. Although its not in razor syntax, its easily convertible.
If you want to create editor template depending on Datatype attribute, you can get the attribute value using ViewData.ModelMetadata.AdditionalValues["DataTypeAttribute"]
If the value is your custom datatype value, do what ever you want. Otherwise, do the default.
Not sure if it is a good idea. But it should work.
You can create a new type, i.e. a Person class that you can include in the model that you send to the view. i.e. Model.Person.
By defining a new EditorTemplate and DisplayTemplate with the name Person. By default, calling #Html.EditorTemplateFor(x=>x.Person); will look for a EditorTemplate for the type.
Have a look at the following stackoverflow post on "How to use asp.net mvc Editor Templates"

How do I bind an image URL in Adobe Flex with data from my database?

Currently i'm retrieving data from my database which is stored in a datagrid. I created a new column which uses an ItemRenderer and inside this I want to show a facebook profile image. I have not stored these inside my database because they can be accessed publicly. I have my user's facebook ID's, so I was hoping to bind this data with the required URL to display the correct image.
I have tried both of the following:
<s:Image id="fbImg" source="http://graph.facebook.com/{getUserResult.lastResult.facebookid}/picture?‌​type=normal"/>
and
<s:Image id="fbImg" source="{'http://graph.facebook.com/' + getUserResult.lastResult.facebookid + '/picture?type=normal'}"/>
I'm getting a 1120 error (access of undefined property) but i'm not sure why. My datagrid is populated using <s:AsyncListView list="{getUserResult.lastResult}"/> and I can manage to pull other data from the getUserResult.lastResult.databasefield commands, but not to display an image. The getUserResult refers to a the result of a php function that is called to retrieve data from the database.
Any help is greatly appreciated. Thankyou.
Most likely your itemRenderer does not have access to the getUserResult service; and that is why you're getting an error that says you're accessing an undefined property.
In the itemRenderer you must use the data property to access the element of your dataProvider that your renderer is attempting to display.
So, most likely you want to do something like this:
<s:Image id="fbImg" source="http://graph.facebook.com/{data.facebookid}/picture?‌​type=normal"/>
However, I strongly advise against using Binding inside an itemRenderer. Instead, modify the image's source property in a dataChange event handler (my preference) or by overriding the set data method. Here are a bunch of links about itemRenderers that may help;
override public function set data(value:Object):void {
if (value != null) {
super.data = value;
this.fbImg.source = "http://graph.facebook.com/" + value.facebookId + "/picture?‌​type=normal value.facebookid";
}
<s:Image id="fbImg" />

model binding url encoded string mvc 3

I am using MVC 3 model binding in combination with JQuery serializer() to edit some data in a form. All is working well except when I have some 'url-encoded' (don't know a better term) text in my data. This data is there because I use a rich text editor just like the one I use now.
$.post("/controller/submit", $("form").serialize(), function (r) {....}
And my controller is like
[HttpPost]
public ActionResult Confirm(MyViewModel model)
{
return PartialView(model);
}
Some data that will cause problems looks like this
Venue=dasd&StartDate=5-sep-2011&startTime=0%3A00&endTime=0%3A00&EndDate=6-sep-2011&Title=Hello&Description=%3Cstrong%3Ebold+mother%3C%2Fstrong%3E&Pricing=&BuyTicketsUrl=&CategoryId=1&Url=&Bid=0&MaximumExpense=0
You can see that the description has stuff like %3Cstrong%3E because it is a serialized textbox with some html encoded text. Can I get the default model binder to just get the html or can I change the way JQuery serializes the form? Or should I use JSON instead?
The .serialize() method does what it is supposed to do. There is nothing else you need to do on the client side. The problem is the server that will reject this input. You could decorate your Description property on the view model with the [AllowHtml] attribute:
[AllowHtml]
public string Description { get; set; }
Now the default model binder will be happy to assign this value. Now because some malicious user can decide to do nasty things to your site if you ever intend to show this HTML unencode make sure you pass it through AntiXss. If you are always going to display this value HTML encoded then you are pretty safe => simply store it as is in the database and display by HTML encoding.

Show Image on Select Change in MVC3 Razor

I have a select list which contains some values representing status of some task.
The values for select list will come from a property in ViewModel. The object representing status also contains path of associate image like:
class Status
{
public int Id{get;set;}
public string Title{get;set;}
public string ImagePath{get;set;}
}
Example values for Rejected status:
var s=new Status{ Id=1, Title="Rejected", ImagePath="~/images/rej.png"};
The image path is relative to root directory.
I want to display image representing the status on change of the select list in an image besides it. What is the best way to do this using MVC3 Razor?
Do you need to store the reject image for each status?
I would have thought something like a reject image would be generic enough to be handled with Css and an image.
All you would do is change the class with javascript when the select is changed.
If you want to update data in the database on the change event then you can use ajax to post a change back.
Maybe I do not fully understand what it is you are trying to achieve.
But if I do then I would have thought the above would be an elegant way to handle the problem?

Resources