render umbraco field in partial view

nickornotto picture nickornotto · Jul 14, 2014 · Viewed 7.3k times · Source

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

Answer

c0r3yz picture c0r3yz · Jul 14, 2014

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