Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get HTMLPREFIX inside the partial view i Razor

Hi I'm in need of getting the Html Prefix inside the partial view.

I've got a prefix like this attached to where I render my partial view

 Html.RenderPartial("Person", Model.Person, new ViewDataDictionary(Html.ViewDataContainer.ViewData)
{
    TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "DelegatePerson"}
});

Now when I go in to the Person PartialView I need the prefix in a situation like this.

@Html.TextBoxFor(model => model.CPR, new { @class = "numericTextbox", maxlength = 10, id = "CprField", onkeyup ="CprFieldEdited("[HTMLPrefix]_FieldName");" })

however I don't know how to do so. and I've been searching for a long time now. I hope someone knows how to do this or something similar.

EDIT:

this means I should be able to tell the Javascript function CprFieldEdited that the html prefix it should search for is in this case "DelegatePerson"

Edit 2: It seems it is not clear what I mean so I'll try to provide some more code.

Main Page:

 //None relevant Code here
@Scripts.Render("~/bundles/person_search")
//None relevant Code here
        <Body>
    //None relevant Code here
               Html.RenderPartial("Person", Model.Person, new ViewDataDictionary(Html.ViewDataContainer.ViewData)
               {
                   TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "DelegatePerson"}
               });
        //None relevant Code here
               Html.RenderPartial("Person", Model.Person, new ViewDataDictionary(Html.ViewDataContainer.ViewData)
               {
                   TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "EmployerPerson"}
               });
        //None relevant Code here
            </body>

in this scenario I've got two Partials of the same type that are rendered on the same page. However inside them I need to tell the Javascript bundles which one of them is being used now as I cannot use the ID's they are being used for something else my idea what to use the Name attribute with the prefix however as the next code shows I can't give the javascript the prefix it's in to differentiated between the two partials

 @Html.TextBoxFor(model => model.CPR, new {id = "CprField", onkeyup ="CprFieldEdited("[FieldNameWithPartialPrefix]");" })

the problem is I cannot give the CprFieldEdited("[FieldNameWithPartialPrefix]"); javascript the "[FieldNameWithPartialPrefix]" which could be either "DelegatePerson_Fieldname" or "EmployerPerson"

like image 219
Helbo Avatar asked Sep 02 '25 10:09

Helbo


1 Answers

You do not need to pass a prefix using the HtmlFieldPrefix. You could just pass it using ViewBag (which works similar to ViewData, but it more recommended).

Just do,

 @Html.RenderPartial(
     "Person", 
     Model.Person, 
     new ViewDataDictionary()
         {
              { "Custom Prefix", "your_prefix" }
         });

Alternatively, you could define your prefix in the partial,

@{
    ViewBag.CustomPrefix = "your_prefix";
}

And to access it either way, just do,

<a onkeyup="@ViewBag.CustomPrefix"> ... </a>

You could use ViewBag to pass anything as it is dynamic. For more info.

like image 136
rae1 Avatar answered Sep 03 '25 23:09

rae1