as a shortcut to HTML Encoding data-bound items. I am trying to use it with a ListView that is data bound by a LINQ query but I am just told "the name X does not exist in the current contxt".
Code Behind:
using (var db = new FullContext())
{
lvSearchResults.DataSource = (from i in db.Items
where i.TypeID == SelectedType || SelectedType == 0
orderby i.Name
select new { ItemName = i.Name,
RegionName = i.Region.Name,
i.Condition
}).ToList();
lvSearchResults.DataBind();
}
Can you translate that to webforms? I am not using MVC.
I think it's good for you to learn the rule that translate betwween webforms and MVC, prehaps it's necessary for programming in the future.
regards
Well seeing as how I don't know anything about MVC that would be hard and it is kind of stupid to have to learn a whole new method of programing just to do a translation.
So can anyone that knows MVC and Webforms translate that for me?
Eagle_f90
Member
465 Points
532 Posts
Help using the new HTML Encoded Data-Binding Expression with LINQ bound Listview
Dec 06, 2012 02:10 PM|LINK
According to http://www.asp.net/vnext/overview/aspnet/whats-new#_Toc318097391 ASP.Net 4.5 you are supposed to be able to use
as a shortcut to HTML Encoding data-bound items. I am trying to use it with a ListView that is data bound by a LINQ query but I am just told "the name X does not exist in the current contxt".
Code Behind:
using (var db = new FullContext()) { lvSearchResults.DataSource = (from i in db.Items where i.TypeID == SelectedType || SelectedType == 0 orderby i.Name select new { ItemName = i.Name, RegionName = i.Region.Name, i.Condition }).ToList(); lvSearchResults.DataBind(); }ASPX code tryed:
DarrellNorto...
All-Star
86685 Points
9634 Posts
Moderator
MVP
Re: Help using the new HTML Encoded Data-Binding Expression with LINQ bound Listview
Dec 07, 2012 10:28 AM|LINK
It is because you are selecting to an anonymous type. When you "select new { }" that is an anonymous type.
Create a class (called a ViewModel) with the fields you want, like this:
public class ItemInfoViewModel { public string ItemName { get; set; } public string RegionName { get; set; } public string ItemCondition { get; set; } }Then "select new ItemInfoViewModel { }" like this:
lvSearchResults.DataSource = (from i in db.Items where i.TypeID == SelectedType || SelectedType == 0 orderby i.Name select new ItemInfoViewModel { ItemName = i.Name, RegionName = i.Region.Name, i.Condition }).ToList();Set your view to be strongly typed, look at the top of your view .cshtml and make sure it looks like this:
Then you can use
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
Eagle_f90
Member
465 Points
532 Posts
Re: Help using the new HTML Encoded Data-Binding Expression with LINQ bound Listview
Dec 09, 2012 05:01 PM|LINK
Can you translate that to webforms? I am not using MVC.
bruce le
Member
298 Points
49 Posts
Re: Help using the new HTML Encoded Data-Binding Expression with LINQ bound Listview
Dec 13, 2012 12:29 AM|LINK
Hi Eagle
I think it's good for you to learn the rule that translate betwween webforms and MVC, prehaps it's necessary for programming in the future.
regards
Eagle_f90
Member
465 Points
532 Posts
Re: Help using the new HTML Encoded Data-Binding Expression with LINQ bound Listview
Dec 13, 2012 03:08 AM|LINK
Well seeing as how I don't know anything about MVC that would be hard and it is kind of stupid to have to learn a whole new method of programing just to do a translation.
So can anyone that knows MVC and Webforms translate that for me?
Angie xu - M...
All-Star
18510 Points
1571 Posts
Microsoft
Re: Help using the new HTML Encoded Data-Binding Expression with LINQ bound Listview
Dec 14, 2012 03:36 AM|LINK
Hi Eagle
You could post these codes to MVC Forum(http://forums.asp.net/1146.aspx), and you will get a quick response.
Moreover, if you have any problems about MVC in your future programming, you could get kind response there.
Thanks for your support to our ASP.NET Forums.
Kind regards
Feedback to us
Develop and promote your apps in Windows Store
Eagle_f90
Member
465 Points
532 Posts
Re: Help using the new HTML Encoded Data-Binding Expression with LINQ bound Listview
Jan 26, 2013 09:46 PM|LINK
The simple solution was to add the eval command
<%#:Eavl("ColumnName")%>