• 自定义模型绑定


     public class GC_ModelBindcs : IModelBinder
        {
            public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
            {
                List<Model> model = (List<Model>)bindingContext.Model ?? new List<Model>();
                // string str = bindingContext.ValueProvider.GetValue("arrMachine[0][status]").AttemptedValue;
                int count = 0;
                if (controllerContext.HttpContext.Request.Form.AllKeys.Length > 0)
                {
                    count = (controllerContext.HttpContext.Request.Form.AllKeys.Length - 1) / 2;
                }
                for (int i = 0; i < count; i++)
                {
                    Model m = new Model();
                    string id_key = bindingContext.ModelName + "[" + i + "]" + "[id]";
                    string status_key = bindingContext.ModelName + "[" + i + "]" + "[status]";
                    m.id = int.Parse(bindingContext.ValueProvider.GetValue(id_key).AttemptedValue);
                    m.status = bool.Parse(bindingContext.ValueProvider.GetValue(status_key).AttemptedValue);
                    model.Add(m);
                }
                return model;
            }
        }
    
    MVC后台:
    
     public ActionResult Index_Post(int groupId, [ModelBinder(typeof(GC_ModelBindcs))]List<Model> arrMachine)
            {
                
                return View();
            }
    
  • 相关阅读:
    向 DataGridView 的行集合中添加行
    添加form窗口最大化最小化事件
    转义字符表
    键盘输入变简单了
    数字9X9的表格
    统计你输入的任意字符
    一个九九表
    用冒泡法排序
    学生会
    任意排序几个数
  • 原文地址:https://www.cnblogs.com/gaocong/p/5550071.html
Copyright © 2020-2023  润新知