PDA

View Full Version : [CLOSED] Problem with namespaces and duplicated ids in Razor



andreasperanza
Jul 24, 2012, 9:02 AM
Hi,

This is my Index View (Model is simply an integer, i.e. = 5)



@model int

@{
ViewBag.Title = "Test with " + Model + " components";
}

@Html.X().ResourceManager()

<div class="page">
@for (int i = 1; i <= Model; ++i)
{
<div>
@Html.Partial("PartialTest", i)
</div>
}
</div>


... and this is the PartialTest View :



@model int

@(Html.X().Panel()
.Namespace("Panel" + Model)
.ID("Panel")
.IDMode(IDMode.Parent)
.Items(it => it.AddRange(new AbstractComponent[] {
@Html.X().Label().Text("Fist Label " + Model).ID("firstLabel"),
@Html.X().Label().Text("Last Label " + Model).ID("lastLabel")
}))
)


Due to duplicated IDs of components, it doesn't work... :-(

I can solve it this way :



@(Html.X().Panel()
.ID("Panel" + Model)
.Items(it => it.AddRange(new AbstractComponent[] {
@Html.X().Label().Text("Fist Label " + Model).ID("firstLabel" + Model),
@Html.X().Label().Text("Last Label " + Model).ID("lastLabel" + Model)
}))
)


But it's awful!!! :-P

Is there a better way to solve it?

Thank you!

Vladimir
Jul 24, 2012, 10:23 AM
Do not set ID or set unique ids. There is no another way

Why do you set ID?

Vladimir
Jul 24, 2012, 10:25 AM
IDMode property has no sense under Razor because Razor doesn't know about controls (control term is absent in Razor)
IDMode should be used under WebForm only

andreasperanza
Jul 24, 2012, 10:45 AM
Why do you set ID?


I need an ID to find it in javascript functions.

I would like to have (in the Index View) something like this:



<script language="javascript" type="text/javascript">
var doSomething = function (arg) {
Panel1.firstLabel.value = 'abc';
Panel3.lastLabel.value = 'def';
}
</script>


instead of



<script language="javascript" type="text/javascript">
var doSomething = function (arg) {
App.Panel1_firstLabel.value = 'abc';
App.Panel3_lastLabel.value = 'def';
}
</script>


but it seems I can't...

Vladimir
Jul 24, 2012, 10:57 AM
You can set ItemID for labels and use


App.Panel1.down("#firstLabel").setText('...');
App.Panel1.down("#lastLabel").setText('...');


ItemID must be unique within own container only (in your case it is Panel)

andreasperanza
Jul 24, 2012, 11:08 AM
WOW!!!

It works!!

THANK YOU !!! ;-)