日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET MVC 2入门演练 3 - 列表和添加功能

發(fā)布時間:2025/5/22 asp.net 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET MVC 2入门演练 3 - 列表和添加功能 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、列表顯示-View:List.aspx

  此功能實現(xiàn)起來比較簡單,之前我們在Site.Master添加了如下一行代碼:

<li><%:?Html.ActionLink("News",?"List",?"News")%></li>

?

  其中"List"就是指定Action,最后面的"News“指定的是Controller,所以,只需要在NewsController中實現(xiàn)List方法即可。以下代碼是NewsController類中的List方法。

//讀取CMSNews表中的所有數(shù)據(jù)行并按ID降序排列
public?ActionResult?List()
{
????
return?View(db.CMSNews.OrderByDescending(Model?=>?Model.ID).ToList());
}

?

  db是在類NewsController中聲明的成員:

MVCDemoEntities?db?=?new?MVCDemoEntities();

?

  MVCDemoEntities哪里來的呢,查看以下Web.config中的鏈接字符串:

?

<add?name="MVCDemoEntities"?connectionString="metadata=res://*/Models.MVCDemoModel.csdl|res://*/Models.MVCDemoModel.ssdl|res://*/Models.MVCDemoModel.msl;provider=System.Data.SqlClient;provider?connection?string=&quot;Data?Source=.\MSSQLSERVER2008;Initial?Catalog=MVCDemo;Integrated?Security=True;MultipleActiveResultSets=True&quot;"?providerName="System.Data.EntityClient"?/>

?

?  ?

?  到此列表顯示功能完成,我這里沒有更改View中的代碼,您可以根據(jù)需要去更改,比如把表格改成div,刪除一些不想再列表中顯示的列等。

 ??? 另外,自動生成的代碼我都沒改,實際上,像<%: Html.ActionLink("Edit", "Edit", new { id=item.ID }) %>ActionLink中的參數(shù)可以自定義的。

?二、實現(xiàn)添加功能 - View:Create.aspx

  1)View中我把自動生成的驗證代碼去掉,使用了JQuery的表單驗證功能,另外<form>也去掉自動生成的代碼而改為下面的代碼:

//執(zhí)行NewsController中的Create方法
<form?id="frmCreate"?action="<%=Url.Action("Create","News")%>"?method="post">

  代碼如下:

?????<!--添加JQuery腳本引用-->
????
<script?src="http://www.cnblogs.com/Scripts/jquery-1.4.1.min.js"?type="text/javascript"></script>
????
<script?src="http://www.cnblogs.com/Scripts/jquery.validate.min.js"?type="text/javascript"></script>
????
<!--JQuery表單驗證腳本-->
????
<script?type="text/javascript">
????????$(document).ready(
????????????
function?()?{
????????????????$(
"#frmCreate").validate({
????????????????????rules:?{
????????????????????????NewsTitle:?{?required:?
true?},
????????????????????????NewsCategory:?{?required:?
true?},
????????????????????????NewsContent:?{?required:?
true?},
????????????????????????PubDate:?{?required:?
true,
????????????????????????????date:?
true
????????????????????????}
????????????????????},
????????????????????messages:?{
????????????????????????NewsTitle:?
'此項不能為空',
????????????????????????NewsCategory:?
'此項不能為空',
????????????????????????NewsContent:?
'此項不能為空',
????????????????????????PubDate:?{
????????????????????????????required:?
'此項不能為空',
????????????????????????????date:?
'日期格式錯誤'
????????????????????????}
????????????????????},
????????????????????success:?
function?(label)?{
????????????????????????label.addClass(
"valid").text("")
????????????????????}
????????????????????
//submitHandler:?function?()?{?alert("操作已完成!")?}
????????????????}
????????????)
????????????}
????????)
????
</script>

?

  NewsController中Create方法實現(xiàn)代碼:

//
????????
//?POST:?/News/Create

????????[HttpPost]
????????
public?ActionResult?Create(FormCollection?collection)
????????{
????????????
try
????????????{
????????????????
//?TODO:?Add?insert?logic?here
????????????????CMSNews?news?=?new?CMSNews();
????????????????news.NewsTitle?
=?collection["NewsTitle"];
????????????????news.NewsCategory?
=?collection["NewsCategory"];
????????????????news.NewsContent?
=?collection["NewsContent"];
????????????????news.PubDate?
=?DateTime.Parse(collection["PubDate"]);
????????????????db.AddToCMSNews(news);
????????????????db.SaveChanges();
????????????????
return?RedirectToAction("List");
????????????}
????????????
catch
????????????{
????????????????
return?View();
????????????}
????????}

?  運(yùn)行后界面,輸入錯誤時:

  

  輸入正確時:

  

  表單中的HTML代碼:

<fieldset>
????????
<legend>Fields</legend>
????????
<div?class="editor-label">
????????????
<%:?Html.LabelFor(model?=>?model.NewsTitle)?%>
????????
</div>
????????
<div?class="editor-field">
????????????
<%:?Html.TextBoxFor(model?=>?model.NewsTitle)%>
????????
</div>
????????
<div?class="editor-label">
????????????
<%:?Html.LabelFor(model?=>?model.NewsCategory)?%>
????????
</div>
????????
<div?class="editor-field">
????????????
<%:?Html.DropDownListFor(model?=>?model.NewsCategory,
????????????????????????????????????????
new?SelectList(new?MVC2Demo.Models.MVCDemoEntities().CMSNewsCategory.ToList(),
????????????????????????
"CategoryCode","CategoryName"),"--?Select?Category?--")%>
????????
</div>
????????
<div?class="editor-label">
????????????
<%:?Html.LabelFor(model?=>?model.NewsContent)?%>
????????
</div>
????????
<div?class="editor-field">
????????????
<%:?Html.TextAreaFor(model?=>?model.NewsContent,?new?{?Style?=?"width:200px;height:100px"?})%>
????????
</div>
????????
<div?class="editor-label">
????????????
<%:?Html.LabelFor(model?=>?model.PubDate)?%>
????????
</div>
????????
<div?class="editor-field">
????????????
<%:?Html.TextBoxFor(model?=>?model.PubDate)?%>
????????
</div>
????????
<p>
????????????
<input?type="submit"?value="Create"?/>
????????
</p>
????
</fieldset>

?

?  CMSNewsCategory是新聞分類表只有兩列:編碼和名稱,編碼是主鍵,只需要Update一下Model,選中此表即可:

  雙擊MVCDemoModel.edmx,在Model Browser中右擊EntityContainer:MVCDemoEntities,右鍵菜單選擇【Update Model From Database】,在出現(xiàn)的窗口中展開表并選中CMSNewsCategory,然后【Finish】。

   下一篇中,實現(xiàn)瀏覽、修改和刪除功能。

轉(zhuǎn)載于:https://www.cnblogs.com/Ferry/archive/2010/05/30/1747570.html

總結(jié)

以上是生活随笔為你收集整理的ASP.NET MVC 2入门演练 3 - 列表和添加功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。