博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MVC中model binding的坑
阅读量:4620 次
发布时间:2019-06-09

本文共 1027 字,大约阅读时间需要 3 分钟。

这两天发现一个model binding中的坑,就是action中的参数名不能和属性重复,否则绑定不了,参数始终是null,

举例说明: 

T_Account类定义如下

public partial class T_Account    {        [Key]        public int Id { get; set; }        [Required]        [StringLength(50)]        public string Account { get; set; }        [StringLength(50)]        public string Name { get; set; }        public bool IsDeleted { get; set; }    }

在Edit视图提交时的处理方法定义

[HttpPost]        [ValidateAntiForgeryToken]        public ActionResult Edit([Bind(Include = "Id,Account,IsDeleted,Name")] T_Account account)        {            if (ModelState.IsValid)            {                db.Entry(account).State = EntityState.Modified;                db.SaveChanges();                return RedirectToAction("Index");            }            return View(account);        }

注意此时Include中有Account属性,参数名也叫account, account始终为null.

 

但如果把参数account改个名字,例如t_account,问题就解决了

public ActionResult Edit([Bind(Include = "Id,Account,IsDeleted,Name")] T_Account t_account)        {

 

转载于:https://www.cnblogs.com/odyssey/p/5720636.html

你可能感兴趣的文章
局部变量和static变量的区别
查看>>
IE下iframe不能正常加载,显示空白
查看>>
mysql服务性能优化—my.cnf配置说明详解
查看>>
洛谷P1908 逆序对
查看>>
noip模拟赛 排列
查看>>
List 中添加多个List集合以及add() 与addAll()的区别
查看>>
如何处理测试人员的流动问题?
查看>>
1.border-image
查看>>
PagerIndicator主题样式修改
查看>>
java中HashMap类用法
查看>>
完整部署CentOS7.2+OpenStack+kvm 云平台环境(2)--云硬盘等后续配置
查看>>
分布式监控系统Zabbix-完整安装记录 -添加端口监控
查看>>
Python之反向迭代
查看>>
STM32F4 输入输出(GPIO)模式理解
查看>>
转义符
查看>>
poj 1019
查看>>
asp.net mvc上传文件
查看>>
bitmq集群高可用测试
查看>>
subline text3利用正则搜索
查看>>
项目管理思考——职责
查看>>