前言:本文主要是说明asp.net中view和controller的一些交互的问题,在网上没有看到很好很全面的,于是根据经验,自己写一点经验,希望能对大家有所帮助。
摘要:主要是为了实现一个有几个输入框和按钮的界面,输入信息后,点击提交按钮,数据被提交到controller,实现与存储文件等的交互。
一,view中js提交内容到controller
cshtml代码:
@using (Html.BeginForm("EditVersion", "Home", FormMethod.Post))
{
<label>VerName:</label>
@Html.TextBox("VerName", "", new { style="width:100%;"});
<br/>
<label>VerCode:</label>
@Html.TextBox("VerCode", "", new { style = "width:100%;"});
<br/>
<label>DownLoadUrl:</label>
@Html.TextBox("DownLoadUrl", "", new { style = "width:100%;"});
<br/>
<label>DesCription:</label>
@Html.TextBox("DesCription", "", new { style="width:100%;"});
<br/>
<input type="submit" value="提交"/>
}
在对应的action中:
<span style="white-space:pre"> </span>string VerName = Request.Form["VerName"];
string VerCode = Request.Form["VerCode"];
string DownLoadUrl = Request.Form["DownLoadUrl"];
string DesCription = Request.Form["DesCription"];
这样子就可以获取到在输入框里面的信息了。
二,view中的js获取到controller中的变量的值
controller中的代码:
public ActionResult EditVersion() {
ViewData["msg"] = "哈哈";
<span style="white-space:pre"> </span>return View();
}
js代码:
alert('@ViewData["msg"]');
这样子在js中就可以通过ViewData获取到controller中的变量msg的值。
相关内容持续更新中.......