PC Review


Reply
Thread Tools Rating: Thread Rating: 4 votes, 2.00 average.

DataBind Repeater to string[] in ViewData without code behind. MVC 2

 
 
Roger Frost
Guest
Posts: n/a
 
      23rd May 2010
Greetings,

I'm coming from a WinForms background and diving into ASP.Net. I'm using:

C#, VS2010, .Net 4.0, ASP.Net, MVC 2.

What I want to do is get a string[] of roles in the controller with:

string[] currentUsersRoles = Roles.GetRolesForUser(User.Identity.Name);

Then I want to attach this to the ViewData object (also in the controller):

ViewData["CurrentUsersRoles"] = currentUsersRoles;

Now, in the View I want to bind a Repeater to the array I fed to ViewData.
Something
like:

<asp:Repeater ID="CurrentUsersRoles" runat="server" DataSourceID="[WHAT GOES
HERE?]">
<HeaderTemplate>
<b>Account Roles:</b>
<br />
</HeaderTemplate>
<ItemTemplate>
<%# [WHAT GOES HERE?] %>
<br />
</ItemTemplate>
</asp:Repeater>

I want to do this without code behind (which I can already do with my
limited knowledge)
because using code behind defeats the purpose of using MVC in the first
place, but that's
just me. Anyhow, this should be easy right? The Repeater exists for the
sole purpose of
displaying collections efficiently, yes? So is there a collection any
simpler or more fundamental
than an array? I suspect some casting will be needed from object back to
string[], but I don't know
where to begin, all I have now is a hammer and they all look like nails to
me.

I've been googling this for hours, but the posts I've seen all want to use
code behind to
acomplish this except this one:

http://forums.asp.net/t/1257598.aspx

I believe she is on the right track...at least it looks good to me, but
being the newbie that I am, I'm
stuck on the line:

<ext:ViewDataSource ID="MyViewDataSource" runat="server"
ViewDataKey="Computers" />

Because evidently "ext" doesn't mean anything in asp.net.

But, back to my point about simple-array-binding-repeater-thingamajigs, this
approach
seems like overkill for such a trivial task anyhow.

Can someone please enlighten me? All productive answers will be extremely
appreciated!

Thanks,
Roger

 
Reply With Quote
 
 
 
 
Roger Frost
Guest
Posts: n/a
 
      6th Jun 2010
Hello,

Just a quick update here, I may have stumbled upon
something that will lead me in the right direction.

I'm going to attempt to use ExpressionBuilder(s) as
described in:

http://weblogs.asp.net/infinitiesloo...onBuilder.aspx

....I will post back with the results as soon as I have
time to try it. I don't fully understand it yet, or
even what it's capable of, but it's something.

In the mean time, I'm still interested in any idea's
regarding the OP (below).

Thanks,
Roger





"Roger Frost" <(E-Mail Removed)> wrote in message
news:F5CCBB56-05D1-4ACF-8D4E-(E-Mail Removed)...
> Greetings,
>
> I'm coming from a WinForms background and diving into ASP.Net. I'm using:
>
> C#, VS2010, .Net 4.0, ASP.Net, MVC 2.
>
> What I want to do is get a string[] of roles in the controller with:
>
> string[] currentUsersRoles =
> Roles.GetRolesForUser(User.Identity.Name);
>
> Then I want to attach this to the ViewData object (also in the
> controller):
>
> ViewData["CurrentUsersRoles"] = currentUsersRoles;
>
> Now, in the View I want to bind a Repeater to the array I fed to ViewData.
> Something
> like:
>
> <asp:Repeater ID="CurrentUsersRoles" runat="server" DataSourceID="[WHAT
> GOES HERE?]">
> <HeaderTemplate>
> <b>Account Roles:</b>
> <br />
> </HeaderTemplate>
> <ItemTemplate>
> <%# [WHAT GOES HERE?] %>
> <br />
> </ItemTemplate>
> </asp:Repeater>
>
> I want to do this without code behind (which I can already do with my
> limited knowledge)
> because using code behind defeats the purpose of using MVC in the first
> place, but that's
> just me. Anyhow, this should be easy right? The Repeater exists for the
> sole purpose of
> displaying collections efficiently, yes? So is there a collection any
> simpler or more fundamental
> than an array? I suspect some casting will be needed from object back to
> string[], but I don't know
> where to begin, all I have now is a hammer and they all look like nails to
> me.
>
> I've been googling this for hours, but the posts I've seen all want to use
> code behind to
> acomplish this except this one:
>
> http://forums.asp.net/t/1257598.aspx
>
> I believe she is on the right track...at least it looks good to me, but
> being the newbie that I am, I'm
> stuck on the line:
>
> <ext:ViewDataSource ID="MyViewDataSource" runat="server"
> ViewDataKey="Computers" />
>
> Because evidently "ext" doesn't mean anything in asp.net.
>
> But, back to my point about simple-array-binding-repeater-thingamajigs,
> this approach
> seems like overkill for such a trivial task anyhow.
>
> Can someone please enlighten me? All productive answers will be extremely
> appreciated!
>
> Thanks,
> Roger


 
Reply With Quote
 
Cubaman
Guest
Posts: n/a
 
      7th Jun 2010
On May 23, 10:56*am, "Roger Frost" <fros...@hotmail.com> wrote:
> Greetings,
>
> I'm coming from a WinForms background and diving into ASP.Net. I'm using:
>
> C#, VS2010, .Net 4.0, ASP.Net, MVC 2.
>
> What I want to do is get a string[] of roles in the controller with:
>
> * * *string[] currentUsersRoles = Roles.GetRolesForUser(User.Identity.Name);
>
> Then I want to attach this to the ViewData object (also in the controller):
>
> * * ViewData["CurrentUsersRoles"] = currentUsersRoles;
>
> Now, in the View I want to bind a Repeater to the array I fed to ViewData..
> Something
> like:
>
> <asp:Repeater ID="CurrentUsersRoles" runat="server" DataSourceID="[WHAT GOES
> HERE?]">
> * * <HeaderTemplate>
> * * * * <b>Account Roles:</b>
> * * * * <br />
> * * </HeaderTemplate>
> * * <ItemTemplate>
> * * * * <%# [WHAT GOES HERE?] %>
> * * * * <br />
> * * </ItemTemplate>
> </asp:Repeater>
>
> I want to do this without code behind (which I can already do with my
> limited knowledge)
> because using code behind defeats the purpose of using MVC in the first
> place, but that's
> just me. *Anyhow, this should be easy right? *The Repeater exists forthe
> sole purpose of
> displaying collections efficiently, yes? *So is there a collection any
> simpler or more fundamental
> than an array? *I suspect some casting will be needed from object back to
> string[], but I don't know
> where to begin, all I have now is a hammer and they all look like nails to
> me.
>
> I've been googling this for hours, but the posts I've seen all want to use
> code behind to
> acomplish this except this one:
>
> http://forums.asp.net/t/1257598.aspx
>
> I believe she is on the right track...at least it looks good to me, but
> being the newbie that I am, I'm
> stuck on the line:
>
> <ext:ViewDataSource ID="MyViewDataSource" runat="server"
> ViewDataKey="Computers" />
>
> Because evidently "ext" doesn't mean anything in asp.net.
>
> But, back to my point about simple-array-binding-repeater-thingamajigs, this
> approach
> seems like overkill for such a trivial task anyhow.
>
> Can someone please enlighten me? *All productive answers will be extremely
> appreciated!
>
> Thanks,
> Roger


Hello Roger.
There is no runat="server" in MVC Views. You don't use aspnet common
controls with MVC, so forget about Repeaters, DataGrids, etc.
Take a deeper look at some MVC tutorials, specially a foreach loop in
a view, in order to get something like a repeater effect.
http://blogs.msdn.com/b/brada/archiv...px?PageIndex=1

Best regards

Oscar Acosta
 
Reply With Quote
 
Roger Frost
Guest
Posts: n/a
 
      8th Jun 2010
Oscar,

Thanks for replying.

While I can guarantee to a complete certainty that the
following is valid in an MVC 2 View aspx page...

<asp:Label ID="mylabel" Text="Some Text" runat="server">
</asp:Label>

....I will not deny that you are correct in that maybe I should
not use common asp.net controls in my MVC 2 application
in the first place. The further along I get the more headaches
arrise. With my WinForms/Database background, coding the
output manually will probably feel more comfortable anyway.
To heck with markup and server tags! (...to a point at least)

Thanks for making me think outside of the box, I'll
put my hammer away now.

-Roger




"Cubaman" <(E-Mail Removed)> wrote in message
news:12a29f00-f27a-493e-b11d-(E-Mail Removed)...
> On May 23, 10:56 am, "Roger Frost" <fros...@hotmail.com> wrote:
>> Greetings,
>>
>> I'm coming from a WinForms background and diving into ASP.Net. I'm using:
>>
>> C#, VS2010, .Net 4.0, ASP.Net, MVC 2.
>>
>> What I want to do is get a string[] of roles in the controller with:
>>
>> string[] currentUsersRoles =
>> Roles.GetRolesForUser(User.Identity.Name);
>>
>> Then I want to attach this to the ViewData object (also in the
>> controller):
>>
>> ViewData["CurrentUsersRoles"] = currentUsersRoles;
>>
>> Now, in the View I want to bind a Repeater to the array I fed to
>> ViewData.
>> Something
>> like:
>>
>> <asp:Repeater ID="CurrentUsersRoles" runat="server" DataSourceID="[WHAT
>> GOES
>> HERE?]">
>> <HeaderTemplate>
>> <b>Account Roles:</b>
>> <br />
>> </HeaderTemplate>
>> <ItemTemplate>
>> <%# [WHAT GOES HERE?] %>
>> <br />
>> </ItemTemplate>
>> </asp:Repeater>
>>
>> I want to do this without code behind (which I can already do with my
>> limited knowledge)
>> because using code behind defeats the purpose of using MVC in the first
>> place, but that's
>> just me. Anyhow, this should be easy right? The Repeater exists for the
>> sole purpose of
>> displaying collections efficiently, yes? So is there a collection any
>> simpler or more fundamental
>> than an array? I suspect some casting will be needed from object back to
>> string[], but I don't know
>> where to begin, all I have now is a hammer and they all look like nails
>> to
>> me.
>>
>> I've been googling this for hours, but the posts I've seen all want to
>> use
>> code behind to
>> acomplish this except this one:
>>
>> http://forums.asp.net/t/1257598.aspx
>>
>> I believe she is on the right track...at least it looks good to me, but
>> being the newbie that I am, I'm
>> stuck on the line:
>>
>> <ext:ViewDataSource ID="MyViewDataSource" runat="server"
>> ViewDataKey="Computers" />
>>
>> Because evidently "ext" doesn't mean anything in asp.net.
>>
>> But, back to my point about simple-array-binding-repeater-thingamajigs,
>> this
>> approach
>> seems like overkill for such a trivial task anyhow.
>>
>> Can someone please enlighten me? All productive answers will be
>> extremely
>> appreciated!
>>
>> Thanks,
>> Roger

>
> Hello Roger.
> There is no runat="server" in MVC Views. You don't use aspnet common
> controls with MVC, so forget about Repeaters, DataGrids, etc.
> Take a deeper look at some MVC tutorials, specially a foreach loop in
> a view, in order to get something like a repeater effect.
> http://blogs.msdn.com/b/brada/archiv...px?PageIndex=1
>
> Best regards
>
> Oscar Acosta


 
Reply With Quote
 
Cubaman
Guest
Posts: n/a
 
      9th Jun 2010
On Jun 8, 11:37*am, "Roger Frost" <fros...@hotmail.com> wrote:
> Oscar,
>
> Thanks for replying.
>
> While I can guarantee to a complete certainty that the
> following is valid in an MVC 2 View aspx page...
>
> <asp:Label ID="mylabel" Text="Some Text" runat="server">
> </asp:Label>
>
> ...I will not deny that you are correct in that maybe I should
> not use common asp.net controls in my MVC 2 application
> in the first place. *The further along I get the more headaches
> arrise. *With my WinForms/Database background, coding the
> output manually will probably feel more comfortable anyway.
> To heck with markup and server tags! *(...to a point at least)
>
> Thanks for making me think outside of the box, I'll
> put my hammer away now.
>
> -Roger
>
> "Cubaman" <oscar.acostamonte...@googlemail.com> wrote in message
>
> news:12a29f00-f27a-493e-b11d-(E-Mail Removed)...
>
> > On May 23, 10:56 am, "Roger Frost" <fros...@hotmail.com> wrote:
> >> Greetings,

>
> >> I'm coming from a WinForms background and diving into ASP.Net. I'm using:

>
> >> C#, VS2010, .Net 4.0, ASP.Net, MVC 2.

>
> >> What I want to do is get a string[] of roles in the controller with:

>
> >> * * *string[] currentUsersRoles =
> >> Roles.GetRolesForUser(User.Identity.Name);

>
> >> Then I want to attach this to the ViewData object (also in the
> >> controller):

>
> >> * * ViewData["CurrentUsersRoles"] = currentUsersRoles;

>
> >> Now, in the View I want to bind a Repeater to the array I fed to
> >> ViewData.
> >> Something
> >> like:

>
> >> <asp:Repeater ID="CurrentUsersRoles" runat="server" DataSourceID="[WHAT
> >> GOES
> >> HERE?]">
> >> * * <HeaderTemplate>
> >> * * * * <b>Account Roles:</b>
> >> * * * * <br />
> >> * * </HeaderTemplate>
> >> * * <ItemTemplate>
> >> * * * * <%# [WHAT GOES HERE?] %>
> >> * * * * <br />
> >> * * </ItemTemplate>
> >> </asp:Repeater>

>
> >> I want to do this without code behind (which I can already do with my
> >> limited knowledge)
> >> because using code behind defeats the purpose of using MVC in the first
> >> place, but that's
> >> just me. *Anyhow, this should be easy right? *The Repeater exists for the
> >> sole purpose of
> >> displaying collections efficiently, yes? *So is there a collection any
> >> simpler or more fundamental
> >> than an array? *I suspect some casting will be needed from object back to
> >> string[], but I don't know
> >> where to begin, all I have now is a hammer and they all look like nails
> >> to
> >> me.

>
> >> I've been googling this for hours, but the posts I've seen all want to
> >> use
> >> code behind to
> >> acomplish this except this one:

>
> >>http://forums.asp.net/t/1257598.aspx

>
> >> I believe she is on the right track...at least it looks good to me, but
> >> being the newbie that I am, I'm
> >> stuck on the line:

>
> >> <ext:ViewDataSource ID="MyViewDataSource" runat="server"
> >> ViewDataKey="Computers" />

>
> >> Because evidently "ext" doesn't mean anything in asp.net.

>
> >> But, back to my point about simple-array-binding-repeater-thingamajigs,
> >> this
> >> approach
> >> seems like overkill for such a trivial task anyhow.

>
> >> Can someone please enlighten me? *All productive answers will be
> >> extremely
> >> appreciated!

>
> >> Thanks,
> >> Roger

>
> > Hello Roger.
> > There is no runat="server" in MVC Views. You don't use aspnet common
> > controls with MVC, so forget about Repeaters, DataGrids, etc.
> > Take a deeper look at some MVC tutorials, specially a foreach loop in
> > a view, in order to get something like a repeater effect.
> >http://blogs.msdn.com/b/brada/archiv...t-mvc-example-...

>
> > Best regards

>
> > Oscar Acosta


Hello Roger:
Here you'll find a good tutorial to get started with mvc. Just try to
don't thing in classic asp.net terms and you'll get it.
http://weblogs.asp.net/scottgu/archi...-tutorial.aspx

Best regards.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Repeater DataBinding on PostBack and base.DataBind() Oleg Microsoft ASP .NET 1 25th Sep 2009 04:37 AM
Repeater ClientID Change after DataBind() (BUG?) Iain Microsoft ASP .NET 0 1st Sep 2006 05:08 PM
How many times does the repeater databind? Arjen Microsoft ASP .NET 1 17th Aug 2005 05:19 PM
Problem: Nested Repeater won't databind()? Colin Nicholls Microsoft ASP .NET 2 12th Jan 2005 06:29 PM
DataBind rows x to y to an ASP.NET repeater Steve Microsoft ADO .NET 2 21st Oct 2003 11:03 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:05 PM.