4

I use umbraco 7 mvc

I simply want to render umbraco field in a partial view. Have no clue how to do it. @CurrentPage or @Umbraco.Field or RenderMacro etc. do not work

I have the following partial view

@model MvcImport.Models.ImportModel
@{
    var formSent = false;
    var success = TempData["Success"];
    if (success != null)
    {
        bool.TryParse(success.ToString(), out formSent);
    }
}

<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@if (formSent)
{

    <p>Thanks, we'll get back to you soon!</p>
}
else
{
    using (Html.BeginUmbracoForm<MvcImport.Controllers.ImportController>("ImportExcel", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        <div>

            @Html.LabelFor(x => x.FileUpload)
            @Html.TextBoxFor(x => x.FileUpload, new { type = "file", name = "Files" })
            @Html.ValidationMessageFor(x => x.FileUpload)

        </div>
        <div>

            @Html.LabelFor(model => model.FileType)<br />
            @Html.DropDownListFor(model => model.FileType, Model.FileTypes)
            @Html.ValidationMessageFor(x => x.FileType)

        </div>
        <input type="submit" value="Submit" class="btn-submit" />
    }
}

I simply want to replace

<p>Thanks, we'll get back to you soon!</p>

with current page field eg.

@currentPage.thankYouCopy

or

@Umbraco.Field("thankYouCopy")

How to do it?

I'm calling the partial in my main view by this:

@Html.Partial("Import", new MvcImport.Models.ImportModel { FileTypes = fileTypes })

Thanks

nickornotto
  • 1,946
  • 4
  • 36
  • 68

2 Answers2

4

I haven't tested this at all, but this should get you going down the right path... Best I can tell, you want to replace this:

@model MvcImport.Models.ImportModel

With this:

@inherits Umbraco.Web.Mvc.UmbracoViewPage<MvcImport.Models.ImportModel>

This should give you access to the UmbracoHelper and allow you to access the property like so:

@Umbraco.Field("thankYouCopy")
c0r3yz
  • 835
  • 8
  • 19
  • 1
    yeahaaa it works. I tried Umbraco.Web.Mvc.UmbracoViewPage probably with @CurrentPage only it's why it didn't work for me before. But works nicely with Umbraco.Field(..) Thanks @c0r3yz! – nickornotto Jul 15 '14 at 07:52
4

We are a few things mixing up here. You have a custom model while you are trying to get data from Umbraco Nodes.

You are using a custom model to pass to your view. Like @c0r3yz mentions you could change the model being passed to the view. If you have a RenderMvcController or a SurfaceController this might be a good idea.

If you are happy with your current controller (whatever this is), you can use do @Model.MyPropertyName to display the wanted value. If you do not want any umbraco functionality (like the current node etc), this is perfectly fine.

All field retrieval options you mention are similar to each other. What they do is retrieve information of the current Node. Because you are using a custom model, you can not use them until you inherit your custom model from an umbraco baseclass like discussed before.

  • @Umbraco.Field("myfield") is a helper method which can assist you with getting and formatting the right field. This is probably the easiest solution for starters. One of the very powerfull features is the recursive = true. See the documentation for more information on this.
  • @CurrentPage.myField return the value of the field (using dynamics). This is easy to write, but might get you in unexpected situations sometimes.
  • @Umbraco.Content.GetPropertyValue("myField") returns the value of the field. Does exactly the same as the partial but using (and returning) strongly typed methods/objects. If you use Visual Studio, you probably love the intellisense part.
  • @Umbraco.Content.GetPropertyValue<ACustomType>("myField") returns the value of the field using ValuePropertyConverters. If you use the previous and don't specify the type, propertyconverters will be used too. But with this notation you can control a littlebit more which type is returned.

I've added some links to the documentation so you can look up more information about the different options.

dampee
  • 3,392
  • 1
  • 21
  • 37
  • Thanks @dampee. You are sure right I'm mixing things. However I can't understand why I can't use custom model in UmbracoTemplatePage? I have a controller which imports data from excel & want to return them into a list to display in a view. However when I use both @model MyModel and @inherits UmbracoTemplatePage directives I get error I cannot use the both and when I use only @model then it shouts 'model passed into the dictionary is of type 'System.Collections.Generic.List`1[MvcImport.Models.CourseImport]', but this dictionary requires a model item of type 'Umbraco.Web.Models.RenderModel' :( – nickornotto Aug 05 '14 at 15:15
  • Just to add I don't want to create umbraco nodes of each row at this stage, I just need to display data on a page or partial view – nickornotto Aug 05 '14 at 15:16
  • I described the above issue here http://stackoverflow.com/questions/25158951/umbracotemplatepage-and-strongly-typed-view-not-working-together. – nickornotto Aug 08 '14 at 12:29