MVC中有一点非常闪瞎人眼的功能就是,可以根据Model与View视图来直接将页面也数据模型进行绑定,那么我们在想客户端发送页面时不需要进行各种控件赋值,不需要操心怎么渲染,当客户提交表单给服务器时也不需要进行过多的处理直接就能叫表单进行模型化。

那么我们在WebFrom中能否实现类似的功能呢,答案是肯定的。

第一步:建立模型。也就是将表单看着是一个对象,建立相关的属性。

namespace Models
{
using System;
using System.Collections.Generic; public partial class CST_cust_info
{
public int cust_id { get; set; }
public string cust_no { get; set; }
public string cust_full_name { get; set; }
public string cust_master_org_no { get; set; }
public string contract_type { get; set; }
public string cust_short_name { get; set; }
public string cust_mail { get; set; }
}
}

  第二步:建立页面与之对应(这里需要理解一个概念就是表单无论怎么变,最终提交到服务器后服务器对于表单是只认name,不认识其他的。如果表单的那么重复他也不管简单的合起来,还有客户端认识的控件就是靠runat=”server“属性和id来识别,所以name和id这两个东西一定需要处理好,当然通常在runat='server'的情况下,系统渲染的时候直接将id赋值给name属性)

<form id="formCC" runat="server"> 

                                    <asp:DropDownList ID="txt_card_type_id"  runat="server">                         </asp:DropDownList>

                                    <asp:DropDownList ID="txt_person_sex" runat="server" Width="94px">                         </asp:DropDownList>

                                     <asp:TextBox ID="txt_brith_date" runat="server"></asp:TextBox>

                                    <asp:TextBox ID="txt_certificate_date" runat="server" ></asp:TextBox>

                                   <asp:TextBox ID="txt_person_no" runat="server"   ></asp:TextBox> </FROM>...

  

 第三步:建立方法 让Model直接渲染到前台(这里只是简单的赋值,如果是通过render就更美妙了)

PS:1、这里面id名字因为项目中容易冲突,所以将这种反射交互的名称换成 Name=>"txt_"+Name模式来进行映射处理

2、这里需要了解的项目中经常使用的控件类型,一般居多的是WebControl 和HtmlInputControl,其中WebControl指的是继承WebControl这个类型的控件,一般有TextBox,Label等,HtmlInputControl控件就是一些<input runat="server">控件,两者区别在于渲染机制不一样,但是最后还是

       /// <summary>
/// 处理一对一信息
/// </summary>
/// <param name="CustPage"></param>
/// <param name="LableName"></param>
/// <param name="CSTModel"></param>
private static void FillMainCust<T>( CustPage CustPage, Dictionary<string, string> LableName, T CSTModel, int? sort = null)
{
if (CSTModel != null)
{
foreach (string key in LableName.Keys)
{
PropertyInfo pinfo = CSTModel.GetType().GetProperty(key); Control txtname = CustPage.FindControl<Control>("txt_" + key));
if (pinfo != null && txtname != null)
{
object PinValue = (pinfo.GetValue(CSTModel) ?? "");
string PinStr = ""; if (PinValue is DateTime || PinValue is DateTime?)
{
PinStr = ((DateTime)PinValue).ToString("yyyy-MM-dd");
}
else
{
if (PinValue is decimal || PinValue is decimal?)
{
PinStr = ((Decimal)PinValue).ToString("0.00");
}
else
{
PinStr = PinValue.ToString();
}
} #region 根据类型赋值
if (txtname is TextBox)
{
((TextBox)txtname).Text = PinStr;
}
else if (txtname is Label)
{
((Label)txtname).Text = PinStr;
}
else if (txtname is HiddenField)
{
((HiddenField)txtname).Value = PinStr;
}
#endregion #region 特殊处理
if (txtname is Label)
{
if (CustDictory.Special_Dictionary.Keys.Contains(key))
{
((Label)txtname).Text = P2PTable.GetSpecialValue(PinStr, CustDictory.Special_Dictionary[key]);
}
} #endregion #region 处理DropDownList赋值
if (txtname is DropDownList)
{ if (((DropDownList)txtname).Items.FindByValue(PinStr) != null)
{
((DropDownList)txtname).SelectedValue = PinStr;
}
else
{
if (((DropDownList)txtname).Items.FindByText(PinStr) != null)
{
((DropDownList)txtname).Items.FindByText(PinStr).Selected = true;
}
} }
#endregion
#region 处理隐藏控件赋值
if (txtname is System.Web.UI.HtmlControls.HtmlInputHidden)
{
((System.Web.UI.HtmlControls.HtmlInputHidden)txtname).Value = PinStr;
}
#endregion }
} }
}

 第四步: 提交时将from表单填充进入Model对象中 PS:1、此时就不需要考虑控件问题 只需要考虑From表单提交的数据即可,这里from表单提交我们已经在前面渲染的时候做好准备了

2、代码中为了节省性能所以就不遍历对象属性了,直接建立字典来保存要取的值,如果是不需要考虑这些可以直接取值

   foreach (string key in LableName.Keys)
{
string value = CustPage.Request.Form["txt_" + key];
PropertyInfo pinfo = custInfo.GetType().GetProperty(key);
if (value != null && pinfo != null)
{ object NewVlue = value.StringtoObject(pinfo);
pinfo.SetValue(custInfo, NewVlue);
}
}

  好~简单的一种反射机制与From表单特性结合的MV已经完成

这个示例我们需要了解的是:1、from表单是根据name进行提交的。

2、服务器控件最终都是要渲染给客户端的,有些控件是服务器不处理的

3、简单的一些反射使用

4、要正确理解服务端与客户端所干的事情

使用WebFrom来模拟一些MVC的MODEL与View的数据交互功能的更多相关文章

  1. MVC中Model,不仅仅只是数据的传递者

    在Model使用的时候很多人回向以前写三层架构一样使用它,将Model作为数据的传递者. 比如常见的写法 public int Id { get; set; } public int RoleId { ...

  2. Spring MVC:Model、View、ModelAndView

    个人理解:View为服务器上的某个文件容器,可以为JSP,FTL等动态页面文件,甚至是媒体文件等等,单单是一个文件.Model的作用是存储动态页面属性,动态页面文件即View可以在Model中获取动态 ...

  3. ASP.NET MVC之model传值view

    控制器中,我们有时会在知道用户名的情况下,再获取相关数据 例如: public ActionResult Index()        {            UserInfo Entity_Tem ...

  4. EL表达式无法获取Spring MVC的Model封装好的数据解决方法

    1.在spring-mvc的配置文件中已经配置jsp的视图解析器 2.在Controller中使用Model的addAttribute方法添加属性name,msg 3.在jsp页面中使用${msg}取 ...

  5. MVC中Control和View之间数据传递的方式

    1:ViewBag和ViewData 具体区别不做讨论,本处只演示ViewData的具体示例: Controler代码:ViewData["Employee"] = emp; Vi ...

  6. ASP.NET MVC 3 Model【通过一简单实例一步一步的介绍】

    今天主要讲Model的两个方面: 1. ASP.Net MVC 3 Model 简介 通过一简单的事例一步一步的介绍 2. ASP.Net MVC 3 Model 的一些验证 MVC 中 Model ...

  7. asp.mvc展示model

    1. ASP.Net MVC 3 Model 简介 通过一简单的事例一步一步的介绍2. ASP.Net MVC 3 Model 的一些验证 MVC 中 Model 主要负责维持数据状态,将数据从数据存 ...

  8. MVC中Model元数据及绑定机制

    ASP.NET MVC的Model为View Model,表示最终呈现在View上的数据,而Model元数据的一个重要的作用在于控制对象在View上的呈现方式.说得更加具体点,就是基于某种数据类型的M ...

  9. Asp.Net Core 入门(四)—— Model、View、Controller

    和我们学习Asp.Net MVC一样,Asp.Net Core MVC的Model.View.Controller也和我们熟悉的Asp.Net MVC中的相似.不同的是我们在使用Asp.Net Cor ...

随机推荐

  1. 外卖的撕‘哔’大战 CSU 1559

                                                      CSU 1559 Time Limit:1000MS     Memory Limit:131072 ...

  2. Unity扩展编辑器--类型3:Custom Editors

    Custom Editors 加速游戏制作过程的关键是为哪些频繁使用的组件创建自定义的编辑器,为了举例,我们将会使用下面这个极其简单的脚本进行讲解,它的作用是始终保持一个对象注视某一点. public ...

  3. Netty笔记--ByteBuf释放

    参考资料:http://www.maljob.com/pages/newsDetail.html?id=394 参考资料:http://www.blogjava.net/liuguly/archive ...

  4. TextView 设置超过几行后显示省略号

    android:lines="5" android:ellipsize="end"

  5. redis 源码分析

    参考: http://redisbook.readthedocs.org/en/latest/index.html http://www.databaseskill.com/3421161/ The ...

  6. -_-#【Backbone】Collection

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 设计模式(四):SIMPLE FACTORY简单工厂模式 -- 创建型模式

    1.定义 简单工厂模式又称静态工厂方法模式.重命名上就可以看出这个模式一定很简单.它存在的目的很简单:定义一个用于创建对象的接口. 2.适用场景 如果一个客户要一款宝马车,一般的做法是客户去创建一款宝 ...

  8. WPF - 这是一堆代码片段的集合

    1. Image的Source设定,引用的图片文件在其他的dll中.图片的Build action : Resource 如果是直接写: Source="/MyProject.Resourc ...

  9. OperationalError:(1054 - "Unknown column 'game.lable1' in 'field list' ")解决办法

    今天白天遇到一个错误,第一次遇到这样的问题,数据库的问题,百度了很多答案也找了很多博客文章看 问题:OperationalError:(1054 - "Unknown column 'gam ...

  10. Object-C 点语法 -- 笔记

    第一种是经典方式, 第一种是点语法.