netcore v2-模型绑定的七种用法

/**********************BindingController.cs**********************************/

using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PocoDemo
{
    public class BindingController:Controller
    {
        //一基本类型绑定
        // binding/repeat?text=dino&number=2
        public IActionResult Repeat(string text,int number)
        {
            return Ok(string.Format("text={0} number={1}", text, number));
        }

        //二 强制从给定源绑定
        //binding/visit?city=newyork
        public IActionResult Visit([FromQuery] string city)
        {
            return Ok(city);
        }

        //三 从头绑定
        //binding/culture
        public IActionResult Culture([FromHeader(Name ="Accept-Language")] string language)
        {
            return Ok(language);
        }

        //四 从请求体绑定
        public IActionResult Print([FromBody] string content)
        {
            return Ok(content);
        }

        //五 绑定复杂类型
        //binding/showperson?name=zs&age=5
        public IActionResult ShowPerson(Person person)
        {
            return Ok(person.Age + "  " + person.Name);
        }

        //六 绑定基本类型的数组
        public IActionResult ShowEmails([Bind(Prefix ="Email")] IList<string> Emails)
        {
            return Ok(string.Join("  ",Emails));
        }

        //七 绑定复杂类型的数组
        public IActionResult ShowPersons( IList<Person> persons)
        {
            return Ok(persons.Count);
        }


    }
}
/*********************************index.cshtml***************************************/

@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
<h1>@ViewData.Model.Title</h1>
<form action="/binding/print" target="_self" method="post">
    <input type="text" name="Title" value="C#" />
    <button type="submit">submit</button>
</form>

<form action="/binding/showemails" target="_self" method="post">
    <input type="text" name="Email" id="email1" />
    <input type="text" name="Email" id="email2" />
    <input type="text" name="Email" id="email3" />
    <button type="submit">submit</button>
</form>

<form action="/binding/showpersons" target="_self" method="post">
    <input type="text" name="persons[0].Age" id="age01" />
    <input type="text" name="persons[0].Name" id="name01" />
    <input type="text" name="persons[1].Age" id="age11" />
    <input type="text" name="persons[1].Name" id="name12" />
    <input type="text" name="persons[2].Age" id="age21" />
    <input type="text" name="persons[2].Name" id="name21" />

    <button type="submit">submit</button>
</form>
@{
}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值