4

In this code model, fields are returning value but when I try to render using @Html.Sitecore().Field its returning null.For experience editor, these fields should be editable.How to do it?

Am new to Sitecore any help or suggestion is much appreciated

        @using Sitecore.Mvc
        @using Sitecore.Mvc.Presentation
        @using Sitecore.Data

       @model Project.Service.Models.Model
      <div>
      <h1>@Html.Sitecore().Field("Property Name")</h1>

      <h4 class="text-danger"><em>
      <span itemprop="streetAddress"> @Model.ProvinceName</span>,
      <span itemprop="addressLocality">@Model.CityName</span>,
      <span itemprop="addressRegion">@Model.ProvinceName</span> 
      @Model.PostalCode</em></h4>
10

@Html.Sitecore().Field("Property Name") only works when your model is Sitecore.Mvc.Presentation.RenderingModel. You are using a custom model.

To use it in your case, you will need to pass in the item like this.

@Html.Sitecore().Field("Property Name", Item)

In your case, you would need to pass in the datasource item. Your model would look like this, notice the InnerItem property and the EditableField property.

namespace Project.Service.Models
{
    using Sitecore.Data.Items;
    using Sitecore.Mvc.Presentation;

    public class Model
    {
        public string ProvinceName { get; set; }
        public string CityName { get; set; }
        public string PostalCode { get; set; }
        public Item InnerItem { get; set; }
    }
}

Then you data call would look like this, returning the model to your view

public Model GetModel()
{
    var datasourceItem = RenderingContext.Current.Rendering.Item;
    return new Model
    {
        ProvinceName = datasourceItem["Province Name"],
        CityName = datasourceItem["City Name"],
        PostalCode = datasourceItem["Postal Code"],
        InnerItem = datasourceItem
    };
}

Then in your view you would get the Item from your model with the innerItem.

@Html.Sitecore().Field("City Name", Model.InnerItem)
| improve this answer | |
  • How to pass an Item I tried this syntax it returns null value only – sitecore-u9 Mar 9 '18 at 19:06
  • Answer updated to show the new model and the data calls. – Chris Auer Mar 9 '18 at 19:34
  • Thanks for great information.Please clarify me few doubts. ---->About accessing city name am I need to use @Html.Sitecore().Field("City Name", Item). --->what does Editable field really do? – sitecore-u9 Mar 9 '18 at 20:59
  • If you just want the value of "City Name". Then just do @Model.CityName. If you want an editable field do @Html.Sitecore().Field("City Field", Model.InnerItem). The only difference between the two is Model.CityField is just the value of your model while @Html.Sitecore().Field("City Field", Model.InnerItem) is an actual editable field from a field called "City Field" on your datasource item. Hope that helps. If you dont want an editable field, just keep using Model.CItyField. – Chris Auer Mar 9 '18 at 21:06
  • Output for my Rendering Context: RenderingContext.Current.Rendering.Item Also I am not getting any values in INNERITEM model element Home (en#1@master), id: {DD2083AF-2FCD-4C7F-8A7D-F32FC5607DA2} Database: "master" Id: "{DD2083AF-2FCD-4C7F-8A7D-F32FC5607DA2}" Language: "en" Name: "Home" Version: "1" – sitecore-u9 Mar 9 '18 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.