mvc基础系列说谈(5)——参数与值传递,提交数据
準(zhǔn)備工作:
現(xiàn)在添加Customer控制器,同時,在創(chuàng)建控制器的時候,添加Create,Delete,Detail動作。
Customer的模型結(jié)構(gòu)為:
Customer類:CustomerID,LastName,FirstName
現(xiàn)在控制器情況為:?
代碼 public?class?CustomerController?:?Controller{
????????public?ActionResult?Index()
????????{
????????????return?View();
????????}
????????public?ActionResult?Details(int?id)
????????{
????????????return?View();
????????}
????????public?ActionResult?Create()
????????{
????????????return?View();
????????}?
????????[AcceptVerbs(HttpVerbs.Post)]
????????public?ActionResult?Create(FormCollection?collection)
????????{
????????????try
????????????{
?????????????????return?RedirectToAction("Index");
????????????}
????????????catch
????????????{
????????????????return?View();
????????????}
????????}
????????public?ActionResult?Edit(int?id)
????????{
????????????return?View();
????????}
????????[AcceptVerbs(HttpVerbs.Post)]
????????public?ActionResult?Edit(int?id,?FormCollection?collection)
????????{
????????????try
????????????{
????????????????return?RedirectToAction("Index");
????????????}
????????????catch
????????????{
????????????????return?View();
????????????}
????????}
}
?
可以看到,除Index,Details動作外,其它的CU全是兩個動作,由于修飾標(biāo)簽的作用而在不同情況下調(diào)用動作來返回視圖。
(一)添加列表
在Views中添加Customer文件夾,然后在此文件夾中添加Index視圖。并指定強(qiáng)類型Customer,并指定自動生成視圖內(nèi)容:List
代碼就不貼了。然后在動作中為視圖指定model
public?ActionResult?Index(){
????IList<Customer>?_list?=?dd.ShowList();
????return?View(_list);
}
?
Index
Create New ? |
現(xiàn)在點(diǎn)擊第一條的詳細(xì),會發(fā)生什么事:
<%=?Html.ActionLink("詳細(xì)",?"Details",?new?{?/*?id=item.PrimaryKey?*/?})%>?
通過ActionLink來重定向到控制器下的Details動作。
public?ActionResult?Details(int?id){
??return?View();
}
?
這個動作接受一個參數(shù),但在列表中沒有提供參數(shù):
| The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in 'Web.Controllers.CustomerController'. To make a parameter optional its type should be either a reference type or a Nullable type. 參數(shù)名: parameters |
因?yàn)樵?/span>Details方法(動作)動作中要求有一個int型參數(shù),但在傳遞時傳遞的是一個Null值,解決:
1 為Details方法的參數(shù)改為可空整形
2 傳遞參數(shù)
現(xiàn)在為Details動作添加視圖,Details。這個就不說了。然后再點(diǎn)詳細(xì):
會提示:未將對象引用設(shè)置到對象的實(shí)例。這是因?yàn)檫€沒有為詳細(xì)視圖返回model實(shí)例。現(xiàn)在在Details方法(動作)中,添加:
public?ActionResult?Details(int??id){
???Customer?customer?=?new?Customer();
???if?(id.HasValue)
???????customer?=?dd.GetCustomer((int)id);
???return?View(customer);
}
?
現(xiàn)在再點(diǎn)詳細(xì)看看,會發(fā)現(xiàn),可以預(yù)覽,但沒有值,這個很容易理解,傳遞的值為Null,所以int? id就為空,所以返回的是一個空實(shí)例(只有實(shí)例的默認(rèn)值)。這時候可以為其指定一個路由默認(rèn)值:
?
new { controller = "News", action = "NewsList", id = "2" },它取的的是id=2這個默認(rèn)值
?
接下來為詳細(xì)傳遞id參數(shù),即在列表視圖中為詳細(xì)鏈接添加參數(shù):
<%= Html.ActionLink("詳細(xì)", "Details", new { id=item.CustomerId })%>
其中id部分與Details動作的參數(shù)名相同。現(xiàn)在的詳細(xì)就可以正常了。
| Index
? ? Details
CustomerId: 3 FirstName: Tom ? |
?
(二)創(chuàng)建Create視圖
在創(chuàng)建過程中,選擇強(qiáng)類型,并選擇Customer實(shí)體。
然后Create視圖的代碼就不貼了。簡單的說明一下:
·驗(yàn)證控件,每個創(chuàng)建的輸入文本框都有相應(yīng)的ValidationMessage
·Form,添加了表單,但沒有指定動作類型,所以這里默認(rèn)的是Post
??? Post:表單在提交時,填寫在表單中的數(shù)據(jù)將在底層發(fā)送到action=“url”中的url去
??? Get:表單在提交時,填寫在表單中的數(shù)據(jù)會和action=“url”中的url編碼在一起
·輸入框,這個會依賴此頁的強(qiáng)類型Model而對應(yīng)生成。
Inherits="System.Web.Mvc.ViewPage<Web.Models.Customer>"
然后看控制器中,可以看到有兩個動作:
public?ActionResult?Create(){
????return?View();
}?
[AcceptVerbs(HttpVerbs.Post)]
public?ActionResult?Create(FormCollection?collection)
{
????try
????{
???????return?RedirectToAction("Index");
????}
????catch
????{
???????return?View();
????}
}
?
第一個沒有參數(shù),返回空視圖,它就是為導(dǎo)航到Create頁。且默認(rèn)Method為get
第二個用于處理創(chuàng)建工作,它的Method被標(biāo)簽修飾為Post,它只接受post動作。
還以列表頁Index為例,當(dāng)點(diǎn)擊
<%= Html.ActionLink("Create New", "Create") %>時,會get到Create動作。這時執(zhí)行第一個動作,返回空視圖(其實(shí)這個視圖與返回一個沒有值的model一樣)
然后在創(chuàng)建時,提交表單,會提交到Create動作,這個時候接愛的謂詞為Post:[AcceptVerbs(HttpVerbs.Post)]
所以,在這個動作中做數(shù)據(jù)添加操作。
(1)參數(shù)為FormCollection collection
這個表單集合包含了post過來的表單元素。
[AcceptVerbs(HttpVerbs.Post)]public?ActionResult?Create(FormCollection?collection)
{
????try
????{
???????Customer?customer?=?new?Customer?
???????{?
???????????FirstName?=?collection["FirstName"],
???????????LastName=collection["LastName"]?
???????};
???????dd.Add(customer);
???????return?RedirectToAction("Index");
????}
????catch
????{
???????return?View();
????}
}
?
(2)參數(shù)為持久實(shí)體
[AcceptVerbs(HttpVerbs.Post)]public?ActionResult?Create(Customer?customer)
{
????try
????{
????????dd.Add(customer);
????????return?RedirectToAction("Index");
????}
????catch
????{
????????return?View();
????}
}
?
這個比較方便。
(3)通過Form方法得到參數(shù)。
string?strFirstName?=?Request.Form["FirstName"].ToString();string?strLastName?=?Request.Form["LastName"].ToString();
?
這個與FormCollection 相同
?
Index
Create New ? |
?
(三)刪除與編輯與上面的同理
例如:刪除可以get方式傳遞一個id值
(四)文件上傳
如果有文件上傳時,要把Form的
enctype="multipart/form-data"
屬性設(shè)置一下。
| HTML enctype 屬性 enctype 屬性 -- 代表HTML表單數(shù)據(jù)的編碼方式 application/x-www-form-urlencoded:窗體數(shù)據(jù)被編碼為名稱/值對.這是標(biāo)準(zhǔn)的編碼格式. multipart/form-data:窗體數(shù)據(jù)被編碼為一條消息,頁上的每個控件對應(yīng)消息中的一個部分. text/plain:窗體數(shù)據(jù)以純文本形式進(jìn)行編碼,其中不含任何控件或格式字符. |
Form的這個屬性的默認(rèn)值是:application/x-www-form-urlencoded
在http頭中可以看到:Content-Type:application/x-www-form-urlencoded
?
<input type="file" name="upfile1" />
注意:上傳控件的name屬性一定要設(shè)置,否則提交附件無效!
(1)以默認(rèn)方式提交文本
FirstName:
LastName:
提交創(chuàng)建:
發(fā)送的數(shù)據(jù)是:
FirstName:松
LastName:武
(2)以默認(rèn)方式提交上傳文件
現(xiàn)在發(fā)現(xiàn),上傳文件不能提交到服務(wù)器。
查看提交的內(nèi)容,可以看到:
FirstName=q1&LastName=q2
兩個文本屬性以kv對傳到服務(wù)器,而附件:
upfile1=C:\Documents and Settings\Administrator\妗岄潰\Image76.gif
只有一個地址而已
(3) 改用multipart/form-data
這個時候,在http頭及提交的數(shù)據(jù)流里可以看到:
?
| Content-Type:multipart/form-data; boundary=---------------------------7daf1ec01dc ? -----------------------------7daf1ec01dc Content-Disposition: form-data; name="FirstName" ? x -----------------------------7daf1ec01dc Content-Disposition: form-data; name="LastName" X Content-Disposition: form-data; name="upfile1"; filename="C:\Documents and Settings\Administrator\妗岄潰\Image76.gif" Content-Type: image/gif 二進(jìn)制 ? |
以上貼出部分內(nèi)容。
現(xiàn)在,全部的值都可以得到了。
?
[AcceptVerbs(HttpVerbs.Post)]public?ActionResult?Create(FormCollection?collection)
{
????try
????{
???????Customer?customer?=?new?Customer?
???????{?
??????????FirstName?=?collection["FirstName"],?
??????????LastName?=?collection["LastName"]?
???????};
???????dd.Add(customer);?
???????if?(Request.Files.Count?>?0)
???????{
???????????Request.Files[0].SaveAs(Server.MapPath("../uploadlist/xx.gif"));
???????}
???????return?RedirectToAction("Index");
????}
????catch
????{
???????return?View();
????}
}
?
?
?轉(zhuǎn)載于:https://www.cnblogs.com/jams742003/archive/2010/02/08/1665773.html
總結(jié)
以上是生活随笔為你收集整理的mvc基础系列说谈(5)——参数与值传递,提交数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: excel单元格下拉菜单
- 下一篇: C/C++ 位操作 总结