您的位置:首页 > Web前端 > HTML

HTML赋值方法练习

2015-06-07 23:23 495 查看
index.cshtml

@{
ViewBag.Title = "Index";
}
@model TestHtmlMethod2.Models.Genre
<h2>Index</h2>
<div>
@Html.TextBox("Title", "")<text>这是一个text框</text>
@Html.TextArea("Desc","描述<br/>",10,80,null)<text>这是一个文本域</text>
@Html.Label("Desc", "显示文字")
@*
@Html.TextBoxFor(m => m.GenreId)
@Html.LabelFor(m=>m.GenreId)<text>自动的查找当前模型中是否有GenreId这个属性,如果有这个属性那么就查询是否有设置固定的显示名称,有设置显示名称就显示出来。</text>
</div>
<div>
@Html.ValidationMessage("Title")<text>显示后台输出的错误</text>
@Html.PasswordFor(m => m.GenreId)*@
@Html.RadioButtonFor(m => m.GenreId,"1")@Html.LabelFor(m => m.GenreId,"颜色")
@Html.ActionLink("回到产品首页", "Index", "Product", new {id="1" },null)<text>传递一个参数过去</text>
@Html.RouteLink("返回", new { action = "Index" })<text>跟ActionLink遵循相同的模式</text>
@Url.Action("Browse", "Store", new { grenre = "Jazz" },null)<text>返回URL路径</text>
@Html.Partial("View")<text>只把部分视图的内容渲染成字符串不会执行操作方法</text>
@{Html.RenderPartial("View");}<text>跟上面一个方面效果一样,都是写入到http流中,但是这个在高流量的网站中性能更好</text>
@Html.Action("ActionView2") <text>获得动作返回的结果,然后输入到http流中</text>
@{Html.RenderAction("ActionView2");}
</div>


HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TestHtmlMethod2.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
ModelState.AddModelError("Title","标题不能为空!");
ModelState.AddModelError("Name","名称不能为空!");
ViewBag.Price = 10.00;
Models.Genre mg = new Models.Genre();
return View(mg);
}
public ActionResult View()
{
ViewBag.Name = "名称";
return View();
}
[ChildActionOnly]//只能通过html.action方法或者renderAction方法来调用,直接访问无效
[ActionName("ActionView2")]//使用自定义的访问名称
public string View2()
{
return "Action方法调用返回的值";
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: