ASP.NET MVC ÈëÃÅ5¡¢ViewÓëViewData
viewÔÚMVCģʽÖÐÓëÓû§½øÐÐ×îÖ±½ÓµÄ½Ó´¥£¬Ëü¸ºÔðÊý¾ÝµÄ³ÊÏÖ¡£ÕâÀïҪעÒâÒ»µã¾ÍÊÇ£¬viewÖ»ÊǸºÔðÊý¾ÝµÄ³ÊÏÖ£¬ËùÒÔÎÒÃÇÓ¦¸ÃÒª¾¡Á¿ÈÃ
viewÖв»Éæ¼°ÒµÎñÂß¼µÄ´¦Àí¡£
ÎÒÃÇÀ´Ìí¼ÓÒ»¸öBlogÊ×Ò³µÄview¡£ÔÚ°²×°ÁËASP.NET MVCºó£¬ÎÒÃÇÔÚÌí¼ÓÐÂÏîÄ¿µÄʱºò¿ÉÒÔ¿´µ½ÓÐMVCµÄviewÄ£°å£º
×¢£ºÈç¹ûÄãµÄÊÇÖÐÎÄ°æ
µÄVS£¬°²×°Íêºó¿ÉÄÜ»á³öÏÖÕÒ²»µ½Õâ¸öÄ£°åµÄÏÖÏó£¬Äã¿ÉÒԲο¼
ÔÚÖÐÎÄ°æVS 08Öа²×°MVC
Õâƪ
ÎÄÕÂÉèÖÃһϡ£
ÆäÖÐMVC View Content PageÊÇÓÐĸ°æÒ³µÄ¡£ÎÒÃÇÔÚViews/HomeĿ¼ÏÂÌí¼ÓÒ»¸öMVC View Content
Page£¬²¢Ñ¡ÔñÎÒÃÇViews/SharedĿ¼ÏµÄSite.Masterĸ°æÒ³£º
public
partial
class
Index : ViewPage
{
}
ASP.NET
MVCĬÈÏÊÇʹÓÃWebFormÀ´×÷ΪviewµÄ¡£ËùÒÔÎÒÃÇ¿´µ½Ð½¨µÄaspxÒ³Ãæ¼Ì³Ð×ÔViewPage£¬Èç¹ûʹÓÃaspxÒ³Ãæ×÷ΪASP.NET
MVCµÄÊÓͼÒýÇ棬ÔòËùÓеÄaspxÒ³Ã涼±ØÐë¼Ì³Ð×ÔViewPage¡£ÎÒÃÇÔÙ¿´Ò»ÏÂViewPage£º
ÎÒÃÇ¿´µ½ViewPage¼Ì³Ð×ÔASP.NET
WebFormµÄPageÒ³£¬»¹ÊµÏÖÁËIViewDataContainer½Ó¿Ú£¬Í¬Ê±»¹ÌṩÁËһЩHelperÀàµÄʵÀý¡£ÎÒÃÇ¿ÉÒÔʹÓÃViewData
À´´ÓControllerÖÐÍùviewÒ³ÃæÖд«µÝÊý¾Ý¡£ÏÂÃæÎÒÃÇÔÚHomeControllerÖеÄIndex
ActionÖÐÈ¡³öPostsÁÐ±í£¬È»ºóÔÚViewÖÐÏÔʾ¡£ÎÒÃÇÏÈÔÚControllerÖÐÈ¡³öÊý¾Ý£¬Ç°Ãæ˵¹ý£¬ÎªÁË·½±ã£¬ÎÒÃÇ»áÖ±½ÓʹÓÃ
BlogEngineµÄModel²ãÀ´×÷ΪÎÒÃÇÕâ¸ö4mvcBlogµÄModel¡£ËùÒÔÎÒÃǵĴúÂëÈçÏ£º
public
ActionResult Index(
int
?
id)
{
ViewData[
"
Title
"
]
=
BlogSettings.Instance.Name;
List
<
IPublishable
>
posts
=
BlogEngine.Core.Post.Posts
.ConvertAll(
new
Converter
<
Post,
IPublishable
>
(
delegate
(Post p) {
return
p
as
IPublishable; }));
int
pageIndex
=
(id
!=
null
&&
id.HasValue
&&
id.Value
>
0
)
?
id.Value :
1
;
int
pageSize
=
Math.Min(posts.Count, BlogSettings.Instance.PostsPerPage);
if
((pageIndex
-
1
)
*
pageSize
+
pageSize
>
posts.Count)
{
&nbs
Ïà¹ØÎĵµ£º
²âÊÔµÄurlµØÖ·ÊÇhttp://www.test.com/testweb/default.aspx, ½á¹ûÈçÏ£º
Request.ApplicationPath: /testweb
Request.CurrentExecutionFilePath: /testweb/default.aspx
Request.FilePath: /testweb/defa ......
ÔÚÃæÊÔµÄʱºò£¬¾³£»áÓöµ½ÕâÑùµÄÎÊÌ⣬ÆäʵÎÒÃÇ»á¶ÔÆäÖеļ¸ÖÖ·½·¨±È½ÏÊìϤ£¬ÒòΪÏîÄ¿Öо³£Ê¹Óᣵ«ÊÇҪȫÃæµÄ»Ø´ðASP.NETÖÐÒ³Ãæ´«ÖµµÄ·½Ê½£¬¹À¼ÆÍùÍùºÜÄÑÈ«Ãæ¡£
Ò».
ʹÓÃQueryString±äÁ¿
¡¡¡¡¡¡¡¡QueryStringÊÇÒ»Öַdz£¼òµ¥Ò²ÊÇʹÓñȽ϶àµÄÒ»ÖÖ´«Öµ·½Ê½£¬µ«ÊÇËü½«´«µÝµÄÖµÏÔʾÔÚä¯ÀÀÆ÷µÄµØÖ·À¸ÖÐ,Èç¹ûÊÇ´«µÝÒ»¸ö»ò ......
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data.Common;
using System.Data;
namespace DownData.dal
{
public static class DBHelper
{
private stati ......
1. ͼƬÎÊÌâ
·Ç³£ºÃ½â¾ö
<img runat="server" src="~/images/ad468x60.gif" alt="" />
¼ÓÉÏrunat="server" £¬Â·¾¶Ê¹Óþø¶Ô·¾¶~/»ù±¾ÉϾÍOKÁË¡£
2. CSSÎļþÒýÓÃ
<link rel="stylesheet" type="text/css" href="style.css" />
ÕâÀïÉñÆæµÄ¾ÍÊÇlinkµÄÎļþµÄhrefÊÇËæ×ű»·ÃÎÊÎļþµÄĿ¼½á¹¹×Ô¶¯µ÷Õû£¬Ëµ°×Á˾ ......
ÔÚWeb³ÌÐòÖУ¬ÎÒÃÇÖªµÀ£¬·þÎñÆ÷¶ËºÍIE¿Í»§¶Ë²»Êdz£Á¬µÄ£¬Ö»ÓзÃÎÊʱ²ÅÁ¬½Ó£¬Ò»µ©Ò³Ãæ¼ÓÔØÍêºó£¬Õâ¸öÁ¬½Ó¾Í»á¶Ï¿ª£¬Ò²¾ÍÊÇ˵£¬·ÃÎÊÒ³ÃæAºó£¬ÔÙ·ÃÎÊÒ³ÃæBʱ£¬·þÎñÆ÷¶ËÊDz»ÖªµÀÄãÒѾ·ÃÎʹýÒ³ÃæAÁË£¬ÉõÖÁ¶ÔÓÚͬһ¸öÒ³Ã棬·þÎñÆ÷¶ËÒ²²»»áÈ¥¼Ç¼ÄãµÄ·ÃÎÊ£¬Ã¿´Î¶¼»áÖØÐÂʵÀý»°·ÃÎʵÄÒ³Ãæ¡£
Õâ¾Í´øÀ´Ò»¸öÎÊÌ⣬Èç¹ûʵÏÖÎÒ·ÃÎÊÁËLo ......