ÔÚ¡¶±Ü¿ªWebFormÌì¿Ó£¬Óµ±§ASP.Net MVC°É¡·Õâƪ²©¿ÍÖÐÎÒ½²µ½ÁËASP.net WebFormÓÉÓÚһЩÏÈÌìµÄ“ÓÕµ¼·¸×ÄȱÏÝ£¬ÏÖÔÚÓÃASP.net MVCµÄ¹«Ë¾Ô½À´Ô½¶à¡£µ«ÊǸù¾ÝÄÇƪÎÄÕÂĩβµÄ"ASP.net MVCµÄÃâ·ÑÍøÂ繫¿ª¿Î"µ÷²é±íµÄͳ¼Æ£¬ÎÒÃÇ·¢ÏÖÓÐÒ»´ó°ëµÄÈË»¹Ã»ÓÐʹÓùýASP.Net MVC£¬¶øûÓùýASP.net MVCµÄÈËÖо¹È»ÓкܶàÈËÈËÊÇÒòΪ¸Ð¾õASP.Net ÄÑ¡¢Ã»Ê±¼äѧ¡£µ÷²é±í·ÖÎöÊý¾ÝÈçÏ£º
³õ¿´ASP.netȷʵÄÑ£º¸´ÔӵķÓÉ»úÖÆ¡¢ ViewData/ViewBag/TempData¡¢¹ýÂËÆ÷¡¢Razor¡¢Layout¡¢XXXHelper¡¢ÑéÖ¤¡¢WebAPI¡¢ÒÀÀµ×¢Èë¡¢µ¥Ôª²âÊÔ……¹â¿´ÕâÒ»¶Ñ¸ÅÄîÍ·¾ÍÔÎÁË£¬“»¹ÊÇÍϿؼþ¼òµ¥”¡£
Æäʵѧϰһ¸öеĿò¼Ü£¬Ö»Òª¸ãÇåËûµÄÔÀí¾Í»á“»íÈ»¿ªÀÊ”£¬ÔÙ¿´ÆäËûµÄ¶«Î÷¾Í²»»á¸Ð¾õ¿Ö¾åÁË¡£
ÕâƪÎÄÕÂÎÒ½«»á´ø×Å´ó¼Ò¸ãÃ÷°×ʲô½ÐMVCģʽ£¬²¢ÇÒ´ø×Å´ó¼ÒÓôó¼ÒÊìϤµÄAsp.Net WebFormʵÏÖMVC£¡
¶Ô£¡ÄãûÌý´í!
ÓÃAsp.Net WebFormʵÏÖMVC£¡
ÔٴθÐÊÜÒ»ÏÂASP.net WebForm°É¡£
¼ÙÉèÓÐÒ»¸öPerson¶ÔÏóµÄ¼¯ºÏ£¬ÎÒÃÇÒªÔÚÍøÒ³ÖÐÒÔhtmläÖȾ£¬ÄÇôҪÈçϱàд
<table> <asp:Repeater runat="server"> <ItemTemplate><tr><td><%#Eval("Name") %></td><td><%#Eval("Age") %></td></tr></ItemTemplate> </asp:Repeater> </table>
¡¡¡¡
C#´úÂ룺
if(!IsPostBack) { List<Person> list = new List<Person>(); list.Add(, Age = 8 }); list.Add(, Age = 18 }); Repeater1.DataSource = list; Repeater1.DataBind(); }
ÕâÑù×öµÄȱµãÊÇC#´úÂëÖзÃÎÊÁËapsxÖеĿؼþRepeater1£¬Ò²¾ÍÊÇÔÚaspxÖбØÐëÒªÓÐÒ»¸öRepeaterÀàÐÍ¡¢IdΪRepeater1µÄ¿Ø¼þ£¬ÕâÑùaspx¾ÍºÍC#´úÂëñîºÏÔÚÁËÒ»Æð¡£Âé·³ÔÚÄĶùÄØ£¿
1£©Èç¹ûaspxÓÐÁ½¸öµØ·½¶¼ÒªÓÃlistÁË£¬ÄÇô¾ÍҪдÁ½×éDataSource=list;DataBind()£»
2£©Èç¹ûaspxÖÐͻȻ²»ÏëÒªRepeaterÁË£¬°ÑRepeater1ɾµôC#´úÂë¾Í»á±¨´í
3£©AspxÖкöÈ»²»ÏëÓÃRepeater½øÐÐÊý¾ÝµÄÏÔʾÁË£¬Ïë»»±ðµÄ¿Ø¼þ£¬ÄÇôC#´úÂëÒ²Òª¸Ä
ÔÙ±ÈÈ磬ÔÚASP.Net WebFormÖУ¬ÊµÏÖ¼Ó·¨¼ÆËãÆ÷»áÈçÏÂʵÏÖ£º
<asp:TextBox runat="server"></asp:TextBox> +<asp:TextBox runat="server"></asp:TextBox> <asp:Button runat="server" Text="=" /> <asp:TextBox runat="server"></asp:TextBox>
¡¡¡¡
protected void Button1_Click(object sender, EventArgs e) { int i1 = Convert.ToInt32(TextBox1.Text); int i2 = Convert.ToInt32(TextBox2.Text); int i3 = i1 + i2; TextBox3.Text = i3.ToString(); }
ºǫ́µÄC#´úÂëºÍaspxµÄñîºÏÒªÇóaspxÊÓͼÖбØÐëÓÐÈý¸öÃû×Ö¸÷ΪTextBox1¡¢TextBox2¡¢TextBox3µÄTextBoxÀàÐ͵ķþÎñÆ÷¿Ø¼þ¡£Èç¹ûÎÒÏë°Ñ¼ÆËãµÄ½á¹û´ÓTextBox¸Ä³Éspan¾Í²»ÐС£Ç°¶ËÉè¼ÆÈËÔ±¿´µ½TextBox¾ø¶ÔûÓÐinputÇ×ÇУ¬Ç°¶ËÈËÔ±¶Ôinput µÄ°Ñ¿ØÄÜÁ¦»á±»TextBox¸üºÃ¡£ÁíÍâ²»±ØÔÙ˵WebFormÒýÈëµÄViewState¡¢Ò³ÃæÉúÃüÖÜÆÚ¡¢ClientIDµÈÁîÈË×÷Å»µÄÎÊÌâ¡£
ÓÐͬѧ»á˵ÁË£ºÄãÓв¡Â𣬿ª·¢Ê±ºòaspxÔõô¿ÉÄÜ×ÜÊDZäÀ´±äÈ¥£¬¼´Ê¹aspx±äÁË£¬ÄãC#´úÂëÒ²¾Í±äߣ¬ÓÐʲô´ó²»Á˵ģ¿
Èç¹û˵ϵͳСµÄ»°¿ÉÄÜÎÞËùν£¬¶ÔÓڱȽϸ´ÔÓµÄϵͳ£¬Èç¹ûaspxºÍC#ÕâÑù½ôÃܵÄñîºÏ£¬Î¬»¤»¹»áÌرðÂé·³¡£¶øÇÒÈç¹ûʵÏÖÏñһЩCMSϵͳÄÇÑù¿ÉÒÔ¶¯Ì¬ÐÞ¸ÄÄ£°åÎļþµÄ»°¾ÍÊÇ´æÔÚ×Å“C#ÎÞ·¨Ô¤²â¡¢ÎÞ·¨Ç¿ÖÆÒªÇóaspxµ½µ×Ôõôд”µÄÎÊÌâ¡£
Õ⻹½ö½öÊÇչʾһ¸ö¼¯ºÏµÄÎÊÌ⣬Èç¹ûҪչʾ¸´Ôӵıâƽ»¯Êý¾Ý»òÕßÐèÒª´ÓÓû§ÊäÈëÖлñÈ¡Êý¾Ý£¬ÓÃÕâÖÖ·½Ê½¸üÔÖÄÑ¡£
ÄÇôMVC˼ÏëÔõô½â¾öÄØ£¿Âß¼´úÂ루Controller£©²»Ö±½ÓºÍÒ³ÃæÊÓͼ£¨View£©½øÐн»»¥£¬ËûÃÇÖ®¼äÓÃModel£¨Êý¾ÝÄ£ÐÍ£©×÷Ϊ¹µÍ¨µÄͨµÀ¡£µ±ÐèҪչʾÊý¾ÝµÄʱºòÓÉControllerÊÕ¼¯µ½Êý¾Ý£¨Model£©£¬È»ºó°ÑÊý¾Ý½»¸øViewȥչʾ£»µ±ControllerÐèÒª¶ÁÈ¡ViewÖÐÓû§ÊäÈëÄÚÈݵÄʱºò£¬¿ò¼Ü»á°ÑViewÖеÄÊý¾ÝÓ³Éäµ½ModelÖУ¬È»ºóController¶ÁÈ¡ModelÖеÄÊý¾Ý½øÐкóÐøµÄÂß¼´¦Àí¡£ÕâÑù¾Í°ÑÂß¼´úÂ루Ë׳ÆC#´úÂ룩ºÍÊÓͼ£¨Ë׳ÆÒ³Ã棩½øÐнâñîÁË¡£
¹â˵¸ÅÄîûÓ㬻¹ÊÇÏÈ¿´´úÂë°Ñ¡£ÏÂÃæʹÓÃMVCģʽ¸ÄÔìµÄ“ÏÔʾPerson¼¯ºÏ”£º
ViewÊÓͼPersonsView.aspx
var persons ]; foreach(var person in persons) { }
×¢Ò⣬ÎÒ°ÑaspxÏà¹ØµÄÁ½¸ö×Ô¶¯Éú³ÉµÄ.csÎļþɾµôÁË£¬²¢ÇÒ°Ñ<%@Page%>±êÇ©µÄAutoEventWireup¡¢CodeBehind¡¢InheritsµÈ¼¸¸öÊôÐÔÒ²¶¼¸ÉµôÁË£¬ÕâÑùaspx¾Í±ä³ÉÁËÒ»¸ö´¿´âµÄ“Ä£°åÒýÇæ”
×÷ΪControllerµÄIndex.ashxµÄ´úÂëÈçÏ£º
public void ProcessRequest(HttpContext context) { List<Person> list = new List<Person>(); list.Add(,Age=8}); list.Add(, Age = 18 }); context.Items[] = list; context.Server.Transfer(); }
ÕâÀï½è¼øÁËJSPÖГrequest.getRequestDispatcher("index.jsp").forward(request,response)”Ò»ÑùµÄ˼·¡£
¡¡