博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

asp.net mvc beta使用心得

Posted on 2008-11-26 11:33  yuanws  阅读(223)  评论(0编辑  收藏  举报

1。        Controllers/HomeController.cs

     //基本数据获取

        public ActionResult Index()
        {
            ViewData["Title"] = "Home Page";
            ViewData["Message"] = "Welcome to ASP.NET MVC!";
            ViewData["users"] = Models.home.GetUserList();   //获取多条记录
            ViewData["city"] = Models.home.GetcityList();    //获取多条记录
            ViewData["link"] = Models.home.GetcityList()[0]; //获取单条记录

           return View()

         }

     //ajax请求的操作

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Add()
        {
            string cityname = Request.Form["username"];
            Models.home.IndertCity(cityname);

            return RedirectToAction("index");
        }

2。Views/Home/Index.aspx

 

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<script src="/mvc/Scripts/jquery-1.2.6.js" type="text/javascript"></script>
    <h2><%= Html.Encode(ViewData["Message"]) %></h2>
    <!--链接-->
    <h2><%=Html.ActionLink("about","about","home") %></h2>
    <h2><%=Html.ActionLink("about2","about") %></h2>
    <h2><%=Html.ActionLink("about", "../home/about")%></h2>
   
    <form action="home/Add" method="post" id="commentform">

    <h2><%=Html.TextBox("username","aa") %></h2>
    <h3>
        <input id="Submit1" type="submit" value="提交" /></h3>
        <h4><%=ViewData["strings"]%></h4>   
    </form>
   
   
    <p>
        To learn more about ASP.NET MVC visit <a href="
http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
    </p><p>
    <% List<MvcApplication1.jz_user> users = ViewData["users"] as List<MvcApplication1.jz_user>;
        foreach(MvcApplication1.jz_user aa in users )
        {
        %><%= aa.name %>
       
      <% }%> </p>
     <p id="cityss">
    <% List<MvcApplication1.jz_city> citys = ViewData["city"] as List<MvcApplication1.jz_city>;
       foreach (MvcApplication1.jz_city aa in citys)
        {
        %><%=aa.name %>
       
      <% }%> </p> 
      <p>
      <%MvcApplication1.jz_city bb = ViewData["link"] as MvcApplication1.jz_city;%>
      <%=bb.name %>
      </p>
<script type="text/javascript" language="javascript">   
//我们只需要在这里注册一下事件就可以

    $(document).ready(function() {

 

        $("#commentform").ajaxForm(success);
        function success(data) {

            if (data != "") { document.getElementById("cityss").innerHTML += document.getElementById("username").value; document.getElementById("username").value = ""; }
        }

    });

</script>  
</asp:Content>