Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 6 VNext how to set HtmlFieldPrefix?

My view is divided into partial views. Hence, at the time of submission my model isn't reconstructed correctly.

The page view displays employee data, where Employee.Contactinfo is the model of _contactInfo partial view, which again has a partial view _phoneInfo to render phone info having model Employee.ContactInfo.PhoneInfo.

Now the problem is with the name of properties. Employee.ContactInfo.PhoneInfo.Contact1 at the time of rendering has name "Contact1", hence at the time of submission the model isn't created appropriately, I get primitive data of Employee but complex type like ContactInfo is null.

I think the solution is to add the prefix at the time of rendering the partial view. How can I perform the following in MVC 6?

employee.cshtml

@model Employee
<% Html.RenderPartial("_conctactInfo", Model.ContactInfo, new ViewDataDictionary
{
    TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "ContactInfo" }
})
%>

_contactInfo.cshtml

@model ContactInfo
<% Html.RenderPartial("_phoneInfo", Model.PhoneInfo, new ViewDataDictionary
{
    TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "PhoneInfo" }
})
%>

_phoneInfo.cshtml

@model PhoneInfo
<input asp-for="@Model.Contact1" />
like image 319
Mukesh Bhojwani Avatar asked Aug 25 '16 12:08

Mukesh Bhojwani


2 Answers

Here is the solution,

namespace Website1.Extensions
{
    public static class HtmlHelper
    {
        public static IHtmlContent Partial(this IHtmlHelper htmlHelper, string partialViewName, object model, string prefix)
        {
            var viewData = new ViewDataDictionary(htmlHelper.ViewData);
            var htmlPrefix = viewData.TemplateInfo.HtmlFieldPrefix;
            viewData.TemplateInfo.HtmlFieldPrefix += !Equals(htmlPrefix, string.Empty) ? $".{prefix}" : prefix;
            return htmlHelper.Partial(partialViewName, model, viewData);
        }

        public static Task<IHtmlContent> PartialAsync(this IHtmlHelper htmlHelper, string partialViewName, object model, string prefix)
        {
            var viewData = new ViewDataDictionary(htmlHelper.ViewData);
            var htmlPrefix = viewData.TemplateInfo.HtmlFieldPrefix;
            viewData.TemplateInfo.HtmlFieldPrefix += !Equals(htmlPrefix, string.Empty) ? $".{prefix}" : prefix;
            return htmlHelper.PartialAsync(partialViewName, model, viewData);
        }
    }
}

employee.cshtml

@using Website1.Extensions;
@model Employee
@Html.Partial("_contactInfo", Model.ContactInfo, nameof(Model.ContactInfo))

_contactInfo.cshtml

@using Website1.Extensions;
@model ContactInfo
@Html.Partial("_phoneInfo", Model.PhoneInfo, nameof(Model.PhoneInfo))

_phoneInfo.cshtml

@model PhoneInfo
<input asp-for="@Model.Contact1" />
like image 127
Mukesh Bhojwani Avatar answered Nov 17 '22 15:11

Mukesh Bhojwani


If you only need this once this would be the quick solution for the _contactInfo partial view

employee.cshtml

   @{
        var viewData = new ViewDataDictionary(ViewData);
        viewData.TemplateInfo.HtmlFieldPrefix = "ContactInfo";
    }

    <partial name="_conctactInfo" model="Model.ContactInfo" view-data="@viewData"/>
like image 5
BergListe Avatar answered Nov 17 '22 17:11

BergListe