NopCommerce架构分析之(五)Model绑定Action参数

2016-10-01 10:04:21 其它

asp.net MVC中Action参数不只是一些基本类型,也支持实体参数。那么从客户端传来的数据如何映射或转换成实体对象呢?就是通过实体绑定类ModelBinder。此系列类在请求转化为后台Controller的Action方法前,捕获传递过来的数据,并对其进行解析和转换,最终为实体类对象。在系统启动前,Global.asax.cs中的方法Application_Start方法调用下面代码定义参数转换规则。//model binders ModelBinders.Binders.Add(typeof(BaseNopModel), new NopModelBinder()); NopModelBinder继承DefaultModelBinder承担系统的实体绑定类,但好像只是留一个接口,并没有使用。主要是继承父类的方法,稍有改变的地方是:方法BindModel添加了对NopModel的绑定支持。public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var model = base.BindModel(controllerContext, bindingContext); if (model is BaseNopModel) ((BaseNopModel) model).BindModel(controllerContext, bindingContext); return model; }方法GetModelProperties添加了一个过滤方法,只是此方法尚未启用。类BaseNopModel是所有Model的基类,支持对自定义属性的存储。并且有一个绑定到解析器的方法BindModel,只是尚未发现有子类实现此方法。

<div class="goodsList_menu"> <div class="goodsList"> <div class="goodsListfl"> <input class="fl" style="margin:15px 10px 0 10px;" type="checkbox" name="" /> <ul> <li>全选</li> <li>商品</li> </ul> </div> <div class="goodsListfr fr"> <ul> <li>单价</li> <li class="w100">数量</li> <li class="w100">小计</li> <li>操作</li> </ul> </div> </div> <!--内容--> <div class='goodsListbox'></div> <div class="checkout"> <div class="checkoutleft"> <input class="fl" style="margin:21px 10px 0 10px;" type="checkbox" name="" /> <ul> <li>全选</li> <li>删除选中产品</li> </ul> </div> <div class="checkoutright fr"> <span>总价:<b>¥</b></span><span class="checkoutSum">0</span> <input type="button" value="去结算" /> </div> </div> </div>body,html,ul,li,a{ margin:0;padding:0;font-family:"microsoft yahei";list-style:none;text-decoration:none;}.fl{ float:left;}.fr{ float:right;}.f12{ font-size:12px;}.disl{ display:inline-block;}.w100{ width:100px;}.fw{ font-weight:bold;}.goodsList_menu{ width:990px;height:45px;background:#f3f3f3;margin:0 auto;line-height:45px; font-size:14px;color:#333;border:1px solid #ddd;}.goodsList_menu .goodsListfl ul li{ float:left;margin-right:80px;cursor:pointer;}.goodsList_menu .goodsListfr ul li{ float:left;margin-right:37px;text-align:center;cursor:pointer;}/*内容*/.goodsList_content{ width:100%;height:80px;border-bottom:1px solid #eee;padding:20px 0;margin-top:40px;}.goodsList_content .disl{ line-height:20px;width:300px;cursor:pointer;margin-left:10px;}.goodsList_content .disl span:hover{ color:#e4393c;}.goodsList_content .disl p:hover{ color:#e4393c;}.goodsListnum .listNum{ widows:45px;height:21px;width:50px;border:1px solid #eee;text-align:center; outline:none;}.goodsListnum ul li{ float:left;margin-right:30px;text-align:center;cursor:pointer;}.goodsListnum ul li a{ border:1px solid #ddd;padding:2px 7px;color:#000;}.goodsListnum ul li p{ line-height:0;color:#666;font-size:12px;margin-top:-2px;}.buy_goods p{ background:url(../images/arrow.png)no-repeat;padding-left:30px;background-position:-20px -20px;}.buy_goods{ position:relative;}/*底部结账*/.checkout{ height:55px;border:1px solid #eee;margin-top:20px;line-height:55px;}.checkout .checkoutleft ul li{ float:left;margin-right:10px;cursor:pointer;}.checkout .checkoutSum{ font-weight:bold;font-size:18px;color:#e64346;}.checkout .checkoutright input{ border:none;color:#fff;outline:none;width:100px;height:55px;line-height:55px; font-size:20px;background:#e64346;margin-left:50px;cursor:pointer;}<script> $.ajax({ type:"get", url:"jd.json", dataType:"json", success:function(data){ var list = data.list;//拿到list数组 //console.log(list); for(var i=0;i<list.length;i++){ var numArray = list[i]; numArray.price = (numArray.price).toFixed(2); var text = "<div class='goodsList_content'><div class='fl buy_goods'>"+ "<input class='fl' style='margin:15px 10px 0 10px;' type='checkbox' name='' />"+ "<img align='top' src='images/1.jpg' /><div class='disl'>"+ "<span>"+numArray.description+"</span><p class='f12'>购买礼品服务</p></div>"+ "<div class='disl' style='width:auto;position:absolute;right:-100px;top:0;'>"+ "<span>"+numArray.color+"</span></div></div>"+ "<div class='fr goodsListnum' style='margin-top:-12px;'>"+ "<ul><li><b>¥</b><span class='fw'>"+numArray.price+"</span></li>"+ "<li class='w100'><a class='minus' onClick='minus(this);' href='javascript:void(0);'>-</a>"+ "<input value='1' class='listNum' /><a class='add' onClick='add(this)' href='javascript:void(0);'>+</a><p>有货</p></li>"+ "<li class='w100'><b>¥</b><span id='sub' class='fw'>"+numArray.price*numArray.quentity+"</span></li>"+ "<li><a style='border:none;color:#666;' href='javascript:void(0);'>删除</a></li>"+ "</ul></div></div>" $(text).appendTo(".goodsListbox"); } }, error:function(){ console.log("调用数据失败!"); } }); var listNum,price,sums,sub1,sub2; function add(obj){ listNum = $(obj).prev().val();//input值 listNum = parseInt(listNum); var num = parseInt(listNum+1);//input值每次+1 $(obj).prev().val(num); price = $(obj).parent().prev().children('span').text();//找到单价 price = parseInt(price);//转换成number类型 price = price.toFixed(2); sums = $(obj).parent().next().children('span').text()//找到总金额 sums = parseInt(sums); console.log(typeof sums); $(obj).parent().next().children('span').text(price*num); sub1 = $(".goodsList_content:eq(0)").find("#sub").text(); sub2 = $(".goodsList_content:eq(1)").find("#sub").text(); sub1 = parseInt(sub1); sub2 = parseInt(sub2); var res = $(".checkoutright .checkoutSum").text((sub1+sub2).toFixed(2)); } function minus(obj){ listNum = $(obj).next().val(); listNum = parseInt(listNum); if(listNum<=1){ listNum==1; }else{ --listNum; } $(obj).next().val(listNum); price = $(obj).parent().prev().children('span').text();//找到单价 price = parseInt(price);//转换成number类型 price = price.toFixed(2); sums = $(obj).parent().next().children('span').text()//找到总金额 sums = parseInt(sums); $(obj).parent().next().children('span').text(price*listNum); $(".checkoutright .checkoutSum").text(price*listNum); sub1 = $(".goodsList_content:eq(0)").find("#sub").text(); sub2 = $(".goodsList_content:eq(1)").find("#sub").text(); sub1 = parseInt(sub1); sub2 = parseInt(sub2); $(".checkoutright .checkoutSum").text(sub1+sub2); } </script>因为时间原因,代码没有进行优化,但是效果还是先给大家呈现出来。希望可以帮到更多的技术爱好者和朋友!如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

复制代码 代码如下:DataTable dt = new DataTable();dt = ds.Tables["All"].Clone();//克隆All的结构传递给dtDataRow[] dr=this.dataSet31.Tables["Product"].Select("bc=1"); //通过条件得到符合条件的行for(int i=0;i<dr.Length;i++){//将数组元素加入表...dt.Rows.Add(dr[i]);//出错提示为:该行已经属于另一个表}解决方法复制代码 代码如下:

全局超时时间 服务器上如果有多个网站,希望统一设置一下超时时间,则需要设置 Machine.config 文件中的 ExecutionTimeout 属性值。 Machine.config 文件位于 %SystemRoot%\Microsoft.NET\Framework\%VersionNumber%\CONFIG\ 目录中。 例如: 复制代码 代码如下: <httpRuntime executionTimeout="90" maxRequestLength="4096" useFullyQualifiedRedirectUrl="false" minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="100" /> 单个站点超时时间 Web.config配置文件中设置http请求运行时间: 复制代码 代码如下: <system.web> <httpRuntime maxRequestLength="102400" executionTimeout="720" /> </system.web> 这里设置的为720秒,前面的属性maxRequestLength一般用于用户上传文件限制大小!默认一般为4096 KB (4 MB)。 单个页面请求超时时间 对于单个页面,可以使用Server.ScriptTimeout来设定超时。 复制代码 代码如下: Server.ScriptTimeout = 120; 注意:如果在Web.config里设置了debug属性,例如:<compilation debug="true" targetFramework="4.0"> 此时,ScriptTimeout会被忽略。

默认支持两个参数: width宽度,default value :250px link 要link的URL对应的Source code是: 复制代码 代码如下: var params = parseQuery( queryString ); if(params['width'] === undefined){params['width'] = 250}; if(params['link'] !== undefined){ $('#' + linkId).bind('click',function(){window.location = params['link']}); $('#' + linkId).css('cursor','pointer'); } 然后我们看到初始化时,是选择所有class=jTip的<a>标签,然后给它们加上hover方法,让click方法失效 复制代码 代码如下: //on page load (as soon as its ready) call JT_init $(document).ready(JT_init); function JT_init(){ $("a.jTip") .hover(function(){JT_show(this.href,this.id,this.name)},function(){$('#JT').remove()}) .click(function(){return false}); } 接着取得当前id的位置计算将要显示DIV的位置,code也不复杂: 复制代码 代码如下: if(hasArea>((params['width']*1)+75)){ $("body").append("<div id='JT' style='width:"+params['width']*1+"px'><div id='JT_arrow_left'></div><div id='JT_close_left'>"+title+"</div><div id='JT_copy'><div class='JT_loader'><div></div></div>");//right side var arrowOffset = getElementWidth(linkId) + 11; var clickElementx = getAbsoluteLeft(linkId) + arrowOffset; //set x position }else{ $("body").append("<div id='JT' style='width:"+params['width']*1+"px'><div id='JT_arrow_right' style='left:"+((params['width']*1)+1)+"px'></div><div id='JT_close_right'>"+title+"</div><div id='JT_copy'><div class='JT_loader'><div></div></div>");//left side var clickElementx = getAbsoluteLeft(linkId) - ((params['width']*1) + 15); //set x position } $('#JT').css({left: clickElementx+"px", top: clickElementy+"px"}); $('#JT').show(); $('#JT_copy').load(url); 其它的一些help function可以参看source code,你可以从这里下载它. 如何使用呢?我们引入它与相应的CSS: 复制代码 代码如下: <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.3.2.min.js"></script> <script src="js/jtip.js" type="text/javascript"></script> 在asp.net webform中使用Handler,asp.net mvc 可以用action.这个用Handler演示: 复制代码 代码如下: [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class DataHandler : IHttpHandler { #region Properties (1) public bool IsReusable { get { return false; } } #endregion Properties #region Methods (2) // Public Methods (1) public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Write(GetData(Convert.ToInt32(context.Request.QueryString["id"]))); } // Private Methods (1) private string GetData(int key) { Dictionary<int, string> mydatadic = new Dictionary<int, string>(); mydatadic.Add(0, "Nothing in life is to be feared. It is only to be understood,this is id equal 0"); mydatadic.Add(1, "<strong>A man</strong> is not old as long as he is seeking something. A man is not old until regrets take the place of dreams. ,this is id equal 1"); mydatadic.Add(2, "A man can succeed at almost anything for which he has unlimited enthusiasm.,this is id equal 2"); mydatadic.Add(3, "To live is to function. That is all there is in living. ,this is id equal 3"); return mydatadic[key]; } #endregion Methods } 好的,在最终的HTML中这么写: 复制代码 代码如下: <span class="formInfo"><a href="DataHandler.ashx?id=0&width=375" class="jTip" id="one" name="Password must follow these rules:">?</a></span> <br> <p><a href="DataHandler.ashx?id=3&width=175&link=http://www.google.com" name="Before You Click..." id="googleCopy" class="jTip">Go To Google</a></p> <a href="DataHandler.ashx?id=3" class="jTip" id="three">Tip No Caption</a> 默认是取name attribute value为Caption,上面是三种典型的link。id是我们自己传给Handler的QueryString,最终效果如下图所示: 希望这篇POST对您有帮助。