asp.net MVC2 初探五

本节讲一些基础性的东西
<%: Html.ActionLink( "修改", "Edit", new { Controller= "Movie",Action= "Edit",id=item.ID})%>
                                |
                                <%: Html.ActionLink( "明细", "Details", new { Controller = "Movie", Action = "Details", id = item.ID })%>
                                |
                                <%: Html.ActionLink( "删除", "Delete", new { id=item.ID })%>
第一二行跨controller,传送数据。第三行不跨,就在当前的控制器找action。
<%@ Page Title= "" Language="C# " MasterPageFile="~/Views/Shared/Site.Master " Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication1.Models.Movie>>" %>

<asp:Content ID= "Content1" ContentPlaceHolderID= "TitleContent" runat= "server">
        Search
</asp:Content>
<asp:Content ID= "Content3" ContentPlaceHolderID= "HeadContent" runat= "server">
        <script language= "javascript" type= "text/javascript">
                 //        function isCheck()
                 //        {
                 //                var rbts = document.getElementsByName("Id");
                 //                for (var i = 0; i < rbts.length; i++) {
                 //                        if (rbts[i].checked) {
                 //                                return true;
                 //                        }
                 //                }
                 //                alert("请选择一行数据");
                 //                return false;
                 //        }
                function isCheck() {
                        var hfd = document.getElementById( "hfdId");
                         if (hfd.value == "") {
                                alert( "请选择一行数据");
                                 return false;
                        }
                }
                function setValue(rbt) {
                        var hfd = document.getElementById( "hfdId");

                        hfd.value = rbt.value;
                }
        </script>
</asp:Content>
<asp:Content ID= "Content2" ContentPlaceHolderID= "MainContent" runat= "server">
        <h2>
                Search</h2>
        <% using (Html.BeginForm( "Edit", "Movie", FormMethod.Get))
             { %>
        <table>
                <tr>
                        <td colspan= "4" style= "text-align: right">
                                <input type= "submit" value= "修改" onclick= "return isCheck();" />
                        </td>
                </tr>
                <tr>
                        <th>
                                选择
                        </th>
                        <th>
                                ID
                        </th>
                        <th>
                                Movie_Name
                        </th>
                        <th>
                                Realease_Date
                        </th>
                </tr>
                <% foreach (var item in Model)
                     { %>
                <tr>
                        <td>
                                <%: Html.RadioButton( "Id", item.ID, new { onclick= "setValue(this);"})%>
                        </td>
                        <td>
                                <%: item.ID%>
                        </td>
                        <td>
                                <%: item.Movie_Name%>
                        </td>
                        <td>
                                <%: String.Format( "{0:g}", item.Realease_Date)%>
                        </td>
                </tr>
                <% } %>
        </table>
        <p>
                <%: Html.Hidden( "hfdId") %>
        </p>
        <p>
                <%: Html.ActionLink( "返回", "Search", "Test")%>
        </p>
        <%} %>
</asp:Content>
这个界面实现根据选中的行,修改值。
修改页面为 using (Html.BeginForm("Edit", "Movie", FormMethod.Get))所指定的action对应的页面。
下面是一个datePick的例子
<%@ Page Title= "" Language="C# " MasterPageFile="~/Views/Shared/Site.Master " Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication1.Controllers.Movies>>" %>

<asp:Content ID= "Content1" ContentPlaceHolderID= "TitleContent" runat= "server">
</asp:Content>
<asp:Content ID= "Content3" ContentPlaceHolderID= "HeadContent" runat= "server">
        <link href= "../../Scripts/DataPicker/css/ui.all.css" />
        <script src= "../../Scripts/jquery-1.3.2.js" type= "text/javascript"></script>
        <script src= "../../Scripts/DataPicker/js/jquery-ui-1.7.1.custom.min.js" type= "text/javascript"></script>
        <script src= "../../Scripts/DataPicker/js/ui.datepicker-zh-CN.js" type= "text/javascript"></script>
        <script src= "../../Scripts/JS1/Common.js" type= "text/javascript"></script>
        <script language= "Javascript" type= "text/javascript">
                function setColor() {
                        var table1 = document.getElementById('table1');
                         for (i = 0; i < table1.rows.length; i++) {
                                (i % 2 == 0) ? (table1.rows[i].className = "t1") : (table1.rows[i].className = "t2");
                        }
                }
        </script>
        <script type= "text/javascript">
                $(function () {


                        $('#startTime').datepicker({
                                changeMonth: true,
                                changeYear: true
                        });
                        $('#endTime').datepicker({
                                changeMonth: true,
                                changeYear: true
                        });
                });
        </script>
</asp:Content>
<asp:Content ID= "Content2" ContentPlaceHolderID= "MainContent" runat= "server">
        <h2>
                ShowDB</h2>
        <% using (Html.BeginForm( "Search", "Test"))
             {%>
        <table id= "table1">
                <tr>
                        <td colspan= "2">
                                按时间从
                        </td>
                        <td colspan= "2">
                                <input type= "text" id= "startTime" name= "startTime" />  至 <input type= "text"
                                        id= "endTime" name= "endTime" />
                                <input type= "submit" id= "btnSearch" value= "查询" />
                        </td>
                </tr>
                <tr>
                        <th>
                        </th>
                        <th>
                                ID
                        </th>
                        <th>
                                MoviewName
                        </th>
                        <th>
                                RealeaseDate
                        </th>
                </tr>
                <% foreach (var item in Model)
                     { %>
                <tr onmouseover= "SetNewColor(this);" onmouseout= "SetOldColor(this);" style= "cursor: hand">
                        <td>
                                <%: item.ID%>
                        </td>
                        <td>
                                <%: item.Movie_Name%>
                        </td>
                        <td>
                                <%: item.Realease_Date%>
                        </td>
                        <td style= "width: 100%">
                                <%: Html.ActionLink( "Edit", "Edit", new { id = item.ID })%>
                                |
                                <%: Html.ActionLink( "Details", "Details", new { id = item.ID })%>
                                |
                                <%: Html.ActionLink( "Delete", "Delete", new { id = item.ID })%>
                        </td>
                </tr>
                <% } %>
        </table>
        <p>
                <%: Html.ActionLink( "Create New", "Create")%>
        </p>
        <div>
        </div>
        <%} %>
</asp:Content>
效果如下,using (Html.BeginForm("Search", "Test", FormMethod.Get, new { autocomplete="off"}))这句用来限制自动完成
点击文本框,弹出如下
控制器代码
public ActionResult Search()
                {
                        ViewData.Model = m1.Movies.ToList();
                         return View();
                }
                [HttpPost]
                 public ActionResult Search(FormCollection fc)
                {
                        DateTime startTime = DateTime.Parse(fc[ "startTime"].ToString());
                        DateTime endTime = DateTime.Parse(Request.Form[ "endTime"].ToString()
                                );
                    
                        var Result = from m in m1.Movies where m.Realease_Date >= startTime && m.Realease_Date <= endTime select m;
                         return View(Result);
                }

你可能感兴趣的:(mvc,职场,asp.net,休闲)