• 控制器读取视图表单中的数据的几种方式


    上次分享了控制器向视图传递数据的4种方式,今天再来给大家讲讲MVC视图中的数据如何提交到控制器。

    我们可以通过以下几种方式提交数据到控制器:

    1、通过Request.Form读取表单数据

    在 控制器动作方法(Action)中,POST方法提交的表单可以使用Request.Form读取其中的数据:

    <html>

    <head></head>

    <body>

    <form>

    <input type="text" name="Name" value="代码里"/>

    <input type="text" name="Site" value="http://www.daimali.com"/>

    <input type="submit" value="提交数据"/>

    </form>

    </body>

    </html>

    在控制器的Action中获取数据:

    public ActionResult  Create()

    {

    string Name=Request.Form["Name"].ToString();

    string Site = Request.Form["Site"].ToString();

    }

    如上,我们实现了数据从视图到控制器的传递,但在ASP.NET MVC中,ASP.NET MVC框架已经提供了DefaultModelBinder类,可以简化表单数据的读取,所以通过Request.Form读取表单数据的方法并不是最好的方式。下面我们再来看看第二种。

    2. 通过FormCollection读取表单数据

    我们可以通过传入的FormCollection集合读取表单数据。

    public ActionResult  Create(FormCollection collection)

    {

    string Name=collection["Name"];

    string Site = collection["Site"];

    }

    3. 通过Model Binder读取表单数据

    模型绑定(Model Binder) 是将浏览器请求的数据映射到模型对象的过程。

    <html>

    <head></head>

    <body>

    <form>

    商品名称:<input type="text" name="Name" value="电脑"/>

    单价:<input type="text" name="Price" value="5200"/>

    <input type="submit" value="提交数据"/>

    </form>

    </body>

    </html>

    在控制器的Action中获取数据:

    public ActionResult  Create(Product product)

    {

    string Name=product.Name;

    float Price =product.Price;

    }

    当 用户提交表单到控制器动作方法中时,将在动作方法中接受一个商品对象作为参数,默认模型绑定会创建商品对象并将HTML表单的字段值赋给该对象属性。

    模型绑定支持类型:简单类型,自定义类,数组,集合,字典

    我们来看看集合的绑定:

    <html>

    <head></head>

    <body>

    <form>

    <input type="checkbox" name="source" value="代码里"/>

    <input type="checkbox" name="source" value="百度"/>

    <input type="checkbox" name="source" value="腾讯"/>

    <input type="submit" value="提交数据"/>

    </form>

    </body>

    </html>

    视图中包含了一个复选框列表,复选框具有相同的名称,在控制器的Action中获取数据:

    public ActionResult  GetCheckBoxList(List<string> source)

    {

    //多选的值自动转换为字符串集合存储在source集合中,可以通过循环获取单个的值

    string message=string.Empty;

    foreach(string msg in source)

    {

    message+=msg;

    }

    .....

    }

    绑定复杂类

    默认模型绑定能够自动绑定大多数复杂类对象

    public class Customer

    {

    public int ID{get;set;}

    public string Name{get;set;}

    public Address Address{get;set;}

    }

    public class Address

    {

    public string City{get;set;}

    public string Country{get;set;}

    }

    Customer类包含一个返回Address类实例的Address属性

    视图表单:

    <html>

    <head></head>

    <body>

    <form>

    <span>客户信息</span>

    名称:<input type="text" name="Name" value="代码里"/>

    <span>客户地址:</span>

    城市:<input type="text" name="Address.City" value="上海"/>

    国家:<input type="text" name="Address.Country" value="中国"/>

    <input type="submit" value="提交数据"/>

    </form>

    </body>

    </html>

    注意:地址表单中都有Address前缀声明

    public ActionResult  Create(Customer customer)

    {

    string name=customer.Name;

    string city=customer.Address.City;

    string Country=customer.Address.Country;

    }

    视图中包含创建新客户的HTML表单,该表单中包含客户表单和客户地址表单两个子表单,地址表单的字段都有Address前缀声明。列如:City属性对应表单字段的Address.City,模型绑定时,自动将表单City字段的值设置到Address.City属性种。

    相关特性使用:

    当然我们在实际使用中可能不需要绑定所有的对象属性,我们可以通过Exclude和Include来实现排除和指定绑定的属性:

    Exclude: 排查绑定的属性列表,列表以逗号隔开

    Include:可以绑定的属性列表,列表以逗号隔开

    列如,不需要绑定ID属性时:

    public ActionResult  Create([Bind(Exclude="ID")]Customer customer)

    {

    string name=customer.Name;

    string city=customer.Address.City;

    string Country=customer.Address.Country;

    }

    当然我们也可以在类中使用绑定特性:

    [Bind(Exclude="ID")]

    public class Customer

    {

    public int ID{get;set;}

    public string Name{get;set;}

    }

    总结:

    1.Model传值: 文本框的name属性值要和model实体的属性名一一匹配,控制器中可以使用实体类的方式接收参数

    2.Request.Form获取Post方式提交的数据

    3.FormConnection集合传值

    原文:代码里(www.daimali.com)原文链接

  • 相关阅读:
    Ckeditor 编辑器上传WPS图片失败问题
    vue3 部署开发环境
    docker 容器报Permission denied问题
    阿里云二级域名解析+Nginx 反向代理,整洁URL
    Linux下安装PostgreSQL
    使用docker 安装 gitlab + jenkins + sonarqube
    Linux下安装Docker
    PL/SQL
    Linux下挖矿病毒解决记录
    Dubbo学习笔记-泛化实现进行mock
  • 原文地址:https://www.cnblogs.com/codelir/p/5552248.html
Copyright © 2020-2023  润新知