在控制器获取View数据的4种方法

  • 传统方法,通过name获取input的值
  • 通过 FormCollection 对象获取值
  • 通过参数获取
  • 构造对象,绑定数据对象

前台代码

<fieldset>
        <legend>Submit data</legend>
    @using (Ajax.BeginForm("SubmitData", "Home",
                            new AjaxOptions {}))
    {
       <div>
             @Html.Label("Name")
                @Html.TextBox("txtName")
           <br />
            @Html.Label("Age")
                @Html.TextBox("txtAge")
       </div>
    <button>Submit</button>
    }   
</fieldset>

界面显示

1、传统方法,通过name获取input的值

       [HttpPost]
        public ActionResult SubmitData()
        {
            string name = Request["txtName"].ToString();
            int age = Convert.ToInt32(Request["txtAge"].ToString());

            string result = "Name:" + name;
            result += "</br>" + "Age:" + age.ToString();
            return Content(result);
        }

结果输出:

2、通过 FormCollection 对象获取值

       [HttpPost]
        public ActionResult SubmitData(FormCollection form)
        {
            string name = form["txtName"].ToString();
            int age = Convert.ToInt32(form["txtAge"].ToString());

            string result = "Name:" + name;
            result += "</br>" + "Age:" + age.ToString();
            return Content(result);
        }

结果输出就不贴了

 

3、通过参数获取

       [HttpPost]
        public ActionResult SubmitData(string txtName, string txtAge)
        {
            string name = txtName;
            int age = Convert.ToInt32(txtAge);

            string result = "Name:" + name;
            result += "</br>" + "Age:" + age.ToString();
            return Content(result);
        }

 

4、构造对象,绑定数据对象(这种是现在比较常用的方法)

前台代码:

@model mvcsample.Controllers.Person
<fieldset>
    <legend>Submit data</legend>
    @using (Ajax.BeginForm("SubmitData", "Home",
                            new AjaxOptions {}))
    {
        <div>
            <div class="editor-label">
                @Html.LabelFor(model => model.Name)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Name)
            </div>
             <div class="editor-label">
                @Html.LabelFor(model => model.Age)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Age)
            </div>
        </div>
        <button>Submit</button>
    }
</fieldset>

后台代码:

 public class HomeController : Controller
    {
        public ActionResult Index()
        {
            Person model = new Person();
            return View(model);
        }

        [HttpPost]
        public ActionResult SubmitData(Person person)
        {
            string name = person.Name;
            int age = person.Age;

            string result = "Name:" + name;
            result += "</br>" + "Age:" + age.ToString();
            return Content(result);
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

  最近在学一下MVC,看了一些国外的文章写得很好,也很详细,就试着翻译一些自认为不错的文章,也当做自己的学习笔记

转载于:https://www.cnblogs.com/bryan2012/p/4081454.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值