4
votes

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

2

2 Answers

4
votes

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")
4
votes

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.