MVC3 view层和controller层数据传输

view到controller:
1.view页面使用<form><input><textbox>等提交,各个提交的数据各自命名;
   controller页面使用"FormCollection form"接收,用form["数据名"]使用数据;
                ex:
                      view页面:
                                   <form>
                                   <input name="searchtype "/>
                                   <input name="nowpage "/> 
                                   <input name="search "/>  
                                  </form>
                      controller页面:
                      public ActionResult Index(FormCollection form)
                                                                   { 
                                                                     string str1=form["searchtype  "];
                                                                     string str2=form["nowpage "]; 
                                                                     string str3=form["search"]; 
                                                                   }
2.view页面使用<form><input><textbox>等提交,各个提交的数据各自命名;
    controller页面使用"数据类型 数据名"形式直接接收并使用数据;
                ex:
                       view页面:
                                   <form>
                                   <input name="searchtype "/>
                                   <input name="nowpage "/> 
                                   <input name="search "/> 
                                  </form>
                      controller页面: 
                       public ActionResult Index (String searchtype, String nowpage,String search)
                                                                    { 
                                                                     string str1=searchtype;
                                                                     string str2=nowpage; 
                                                                     string str3=fsearch; 
                                                                    } 
3.view页面使用<form><input><textbox>等提交,各个提交的数据各自命名;
    controller页面使用Request.Form["数据名"]形式直接接收并使用数据;
                ex:
                       view页面:
                                   <form>
                                   <input name="searchtype "/>
                                   <input name="nowpage "/> 
                                   <input name="search "/> 
                                  </form>
                      controller页面: 
                       public ActionResult Index ()
                                                            { 
                                                                     string str1=Request.Form["searchtype "];
                                                                     string str2=Request.Form["nowpage "]
                                                                     string str3=Request.Form["fsearch "]
                                                             }  
4.  model页面创建一个model
     view页面引用此模型,并使用htmlhelper和lamdba表达式提交数据
     controller页面接收model,并用model变量名.数据名形式使用数据;
                ex:
                  model页面:
                                          public class LogOnModel
                                           {
                                           
public string User { get; set; }
                                            } 
 
                       view页面:
                                         @model 项目名.Models.LogOnModel 
                                                    @using (Html.BeginForm()) {
                                                                  @Html.LabelFor(m => m.UserName)
                                                                                                 } 
                      controller页面: 
                       public ActionResult Index (LogOnModel model)
                                                                    { 
                                                                     string str=model.UserName ;
                                                                    } 

__________________________________________________________________________________________________________________________________________________
__________________________________________________________________________________________________________________________________________________
__________________________________________________________________________________________________________________________________________________


 controllerview: 
1.controller使用ViewData存储数据
   view页面读取相应viewdata
                 ex:
                       controller页面:
                            public ActionResult UsingViewData(){ 
                                         ViewData["Title"] = " Using ViewData";
               
                                         Dictionary<stringstring> stackholder = new Dictionary<stringstring>();
                                         stackholder.Add("Client","Mr. Client"); 
                                         stackholder.Add("Manager","Mr. Joy"); 
                                         stackholder.Add("Team Leader","Mr.Toy"); 
                                         ViewData["stackholder"] = stackholder;
                                           
                                         return View();
                                                                                    }
                      view页面: 
                                   <h1>@ViewData["Title"]</h1>
                                   <ul id="stakholder"> 
                                   @foreach var stakerholder in ViewData["stackholder"as Dictionary<stringstring> )
                                       { 
                                         <li
                                         @stakerholder.Key 
                                         &nbsp; 
                                         @stakerholder.Value
                                        </li>
                                        }
                                    </ul>

2.controller使用ViewBag存储数据
    view页面读取相应ViewBag
                 ex:
                       controller页面:
                            public ActionResult UsingViewBag(){ 
                                         ViewBag.Title= " Using ViewBag ";
               
                                         Dictionary<stringstring> stackholder = new Dictionary<stringstring>();
                                         stackholder.Add("Client","Mr. Client"); 
                                         stackholder.Add("Manager","Mr. Joy"); 
                                         stackholder.Add("Team Leader","Mr.Toy"); 
                                         ViewBag.stackholder= stackholder;
                                           
                                         return View();
                                                                                    }
                      view页面: 
                                   <h1>@ViewBag.Title </h1>
                                   <ul id="stakholder"> 
                                   @foreach var stakerholder iin ViewBag.stackholder)
                                       { 
                                         <li
                                         @stakerholder.Key 
                                         &nbsp; 
                                         @stakerholder.Value
                                        </li>
                                        }
                                    </ul>


3. controller页面使用model存储数据,将model返回到view层
    view层引用次model
                ex:
                  model页面:
                                          public class LogOnModel
                                           {
                                           
public string User { get; set; }
                                            } 
 
                      controller页面: 
                       public ActionResult Index ()
                                                               {
                                                                   LogOnModel model=new LogOnModel();
                                                                   model.UserName="str';
                                                                   return View(model);
                                                               } 
                       view页面:
                                         @model 项目名.Models.LogOnModel 
                                                    @using (Html.BeginForm()) {
                                                                  @Html.LabelFor(m => m.UserName)
                                                                                                 } 
4.  controller页面使用session存放数据
     view页面使用session读取数据
                 ex:
                       controller页面:
                       public ActionResult Index ()
                                  { 
                                    session["searchtype"]="str"                          
                                  } 
                      view页面: 
                                   <form>
                                   @session["searchtype"];
                                  </form> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值