前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >.NET MVC第五章、模型绑定获取表单数据

.NET MVC第五章、模型绑定获取表单数据

作者头像
红目香薰
发布2022-11-30 19:20:23
6120
发布2022-11-30 19:20:23
举报
文章被收录于专栏:CSDNToQQCodeCSDNToQQCode

.NET MVC第五章、模型绑定获取表单数据


目录

.NET MVC第五章、模型绑定获取表单数据

Html.ActionLink超链接

Html.ActionLink示例

HTML辅助方法生成表单


Html.ActionLink超链接

输出超链接使用的HTML辅助方法是Html.ActionLink,常见的有以下3种写法:

代码语言:javascript
复制
@Html.ActionLink("超链接1","函数名称")

@Html.ActionLink("超链接2","函数名称","控制器名称")

@Html.ActionLink("超链接3","函数名称", new { userName = "admin", age = 12 })

Html.ActionLink示例

控制器

代码语言:javascript
复制
public ActionResult Index(string userName,int? age=2)
{
    ViewBag.userName = userName;
    ViewBag.age = age;
    return View();
}

视图层 

代码语言:javascript
复制
@Html.ActionLink("超链接1", "Index")
<hr />
@Html.ActionLink("超链接2", "Index", "Test")
<hr />
@Html.ActionLink("超链接3", "Index", new { userName = "admin", age = 12 })

<hr/>
@ViewBag.userName
<br/>
@ViewBag.age

效果:

链接1、2直接访问,在url上可以看到,并且age的默认值是2,链接3显示admin与age的12,说明超链接符合预期。

HTML辅助方法生成表单

HTML辅助方法

说明

Html.BeginForm()

输出<form>标签

Html.CheckBox()

输出<input type="checkbox">标签

Html.DropDownList()

输出<select>标签

Html.Password()

输出<input type="password">标签

Html.RadioButton()

输出<input type="radio">标签

Html.TextArea()

输出<textarea/>标签

Html.TextBox()

输出<input type="text">标签

Html.Hidden()

输出<input type=”hidden”/>标签

示例

控制器

代码语言:javascript
复制
[HttpPost]
public ActionResult GetForm(string userName,string pwd,string sex,String []likes, string age,string introduce) {
    string info = "";
    info += userName+"<br/>";
    info += pwd + "<br/>";
    info += sex + "<br/>";
    foreach (string l in likes) {
        info += l + "<br/>";
    }
    info += age + "<br/>";
    info += introduce + "<br/>";
    TempData["show"] = info;
    return Redirect("~/Test/Index");
}

视图层

代码语言:javascript
复制
@{
    ViewBag.Title = "Index";
}

<h2>超链接</h2>
<hr />
<form action="~/Test/GetForm" method="post">
    <fieldset>
        <legend>表单</legend>
        <hr />
        <p>
            @Html.TextBox("userName", "", new { @placeholder = "请输入用户名" })
        </p>
        <p>
            @Html.TextBox("pwd", "", new { @placeholder = "请输入用密码" })
        </p>
        <p>
            @Html.RadioButton("sex", "男") 男
            @Html.RadioButton("sex", "女") 女
        </p>
        <p>
            @Html.CheckBox("likes", "0") 健身
            @Html.CheckBox("likes", "1") 交友
            @Html.CheckBox("likes", "2") 蹦迪
        </p>
        <p>
            @Html.DropDownList("age",
           new List<SelectListItem> {
               new SelectListItem { Text = "22岁", Value = "22" } ,
               new SelectListItem { Text = "23岁", Value = "23" } ,
               new SelectListItem { Text = "24岁", Value = "24" }
           }
            , new { @class = "form-control" })
        </p>
        <p>
            @Html.TextArea("introduce")
        </p>
        <p>
            <input type="submit" value="提交" class="btn btn-block btn-primary" />
        </p>
    </fieldset>
</form>

<hr />
@TempData["show"]
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-09-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • .NET MVC第五章、模型绑定获取表单数据
  • Html.ActionLink超链接
    • Html.ActionLink示例
    • HTML辅助方法生成表单
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档