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"];
}
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)
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;
}
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 ()
ex:
view页面:
<form>
<input name="searchtype "/>
<input name="nowpage "/>
<input name="search "/>
</form>
controller页面:
public ActionResult Index ()
{
string str1=Request.Form["searchtype "];
string str1=Request.Form["searchtype "];
string str2=Request.Form["nowpage "];
string str3=Request.Form["fsearch "];
}
4. model页面创建一个model
view页面引用此模型,并使用htmlhelper和lamdba表达式提交数据
controller页面接收model,并用model变量名.数据名形式使用数据;
view页面引用此模型,并使用htmlhelper和lamdba表达式提交数据
controller页面接收model,并用model变量名.数据名形式使用数据;
ex:
model页面:
public string User { get; set; }
}
model页面:
public class LogOnModel
{public string User { get; set; }
}
view页面:
@model 项目名.Models.LogOnModel
@using (Html.BeginForm()) {
public ActionResult Index (LogOnModel model)
@model 项目名.Models.LogOnModel
@using (Html.BeginForm()) {
@Html.LabelFor(m => m.UserName)
}
controller页面: }
public ActionResult Index (LogOnModel model)
{
string str=model.UserName ;
}
__________________________________________________________________________________________________________________________________________________
string str=model.UserName ;
}
__________________________________________________________________________________________________________________________________________________
__________________________________________________________________________________________________________________________________________________
__________________________________________________________________________________________________________________________________________________
controller到view:
1.controller使用ViewData存储数据
view页面读取相应viewdata
ex:
controller页面:
public ActionResult UsingViewData(){
ViewData["Title"] = "
Using ViewData";
Dictionary<string, string>
stackholder = new Dictionary<string, string>();
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<string, string>
)
{
<li>
@stakerholder.Key
:
@stakerholder.Value
</li>
}
</ul>
2.controller使用ViewBag存储数据
view页面读取相应ViewBag
ex:
controller页面:
public ActionResult UsingViewBag(){
ViewBag.Title= " Using ViewBag ";
Dictionary<string, string>
stackholder = new Dictionary<string, string>();
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
:
@stakerholder.Value
</li>
}
</ul>
3. controller页面使用model存储数据,将model返回到view层
view层引用次model
ex:
model页面:
public string User { get; set; }
}
model页面:
public class LogOnModel
{public string User { get; set; }
}
controller页面:
public ActionResult Index ()
public ActionResult Index ()
{
LogOnModel model=new LogOnModel();
model.UserName="str';
model.UserName="str';
return View(model);
}
}
view页面:
@model 项目名.Models.LogOnModel
@using (Html.BeginForm()) {
@model 项目名.Models.LogOnModel
@using (Html.BeginForm()) {
@Html.LabelFor(m => m.UserName)
}
}
4. controller页面使用session存放数据
view页面使用session读取数据
ex:
controller页面:
public ActionResult Index ()
{
session["searchtype"]="str"
session["searchtype"]="str"
}
view页面:
<form>
@session["searchtype"];
</form>
@session["searchtype"];
</form>