asp.net mvc beta使用心得
2008-11-26 11:33
375 查看
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>
//基本数据获取
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>
相关文章推荐
- ASP.NET MVC TempData使用心得(转)
- ASP.NET MVC TempData使用心得
- ASP.NET MVC 3 Beta: Built-in support for charts(MVC3 Razor中使用图表的最佳方案)
- ASP.NET MVC 3 Beta: Built-in support for charts(MVC3 Razor中使用图表的最佳方案)
- ASP.NET MVC 3 Beta: Built-in support for charts(MVC3 Razor中使用图表的最佳方案)
- asp.net mvc使用的心得和问题的解决方法(陆续更新ing)
- ASP.NET MVC 3 Beta: Built-in support for charts(MVC3 Razor中使用图表的最佳方案)
- ASP.NET MVC 3 Beta: Built-in support for charts(MVC3 Razor中使用图表的最佳方案)
- ASP.NET MVC 学习心得 (3) – 怎样使用服务器控件
- ASP.NET MVC 3 Beta: Built-in support for charts(MVC3 Razor中使用图表的最佳方案)
- ASP.NET MVC如何使用Ajax的辅助方法
- Pro ASP.NET MVC –第五章 使用Razor
- 主攻ASP.NET.4.5.1 MVC5.0之重生:在项目中使用zTree jQuery 树插件
- ASP.NET MVC---分部类的使用
- 在 Asp.NET MVC 中使用 SignalR 实现推送功能
- 从零开始写C# MVC框架之--- 使用Areas分离ASP.NET MVC项目
- ASP.NET MVC 5 (六-2) 使用Razor表达式
- 今日工作心得:asp.net中使用javascript进行验证的注意点
- 在ASP.NET MVC环境中使用加密与解密
- Asp.net MVC 3使用 Area 小结