1.超链接:@Html.ActionLink("name","method","controller"); eg:@Html.ActionLink("超链接","Index","Home")
2.输出:@{ Response.Write("输出内容") };只能在页面最上面输出
3.指定位置输出:@Html.Raw("输出内容");
4.页面横线:</hr>
5.表单提交:将表单中的数据,以post的方式提交给Home的controller中的index方法中
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@Html.TextBoxFor(p=>p.Id)
}
6.异步请求1:
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
public ActionResult Add(int a,int b)
{
int sum = a + b;
return Content(sum.ToString());
}
<script>
$(function () {
$('#add').click(function () {
$.post(
'@Url.Action("Add","Home")',
$('#form1').serialize(),
function (msg) {
$('#sum').val(msg);
}
);
});
});
</script>
<form id="form1">
<input type="text" name="a" />+
<input type="text" name="b" />
<input type="button" id="add" value="等于" />
<input type="text" name="sum" id="sum" />
</form>
7.异步请求2:
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
public ActionResult Add1(int a,int b)
{
int sum = a + b;
var temp = new
{
Sum = sum
};
return Json(temp,JsonRequestBehavior.AllowGet);
}
<script>
function Success(obj) {
$('#result').val(obj.Sum);
}
</script>
@using (Ajax.BeginForm("Add1", "home", new AjaxOptions()
{
OnSuccess = "Success"
}))
{
<input type="text" name="a" />
<span>+</span>
<input type="text" name="b" />
<input type="submit" value="等于" />
<input type="text" id="result" />
}
8.校验
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@using (Html.BeginForm("Add", "Person", FormMethod.Post))
{
@Html.TextBoxFor(p=>p.Id)
@Html.ValidationMessageFor(p=>p.Id)--校验提示信息
<br />
@Html.TextBoxFor(p=>p.Name)
<br />
<input type="submit" value="提交" />
}
//[Required]//必须的
//[StringLength:]//字符串长度
//[Range]//int等范围
//[RegularExpression]///正则表达式
//属性ErrorMessage指定错误提示信息
[Required(ErrorMessage = "编号不能为空")]
[Range(10,100,ErrorMessage = "编号必须是10到100之间的值")]
public int Id { get; set; }