JsonResult应用实例
情景:两个下拉选择,后一个下拉选择根据前一个的选择来生成选项。
controller
[ HttpPost ] public JsonResult GetChildrenTypes (string parent) { var result = client. ListProductTypeSub ( parent); if (! result .Success ) { JsError (result . Message); } var data = new JsonResult (); data .Data = result .Result ; return data ; }
view
@{ var itemsProductType = new SelectList ( ViewBag. ListProductType,"Key","Value",Model . ProductType); } < label for="ProductType"> 产品类别 </label > @ Html. DropDownListFor ( m => m . ProductType,@itemsProductType,"请选择",new{@class="input-medium"} ) < label for="ProductTypeSub"> 产品类型</ label>< select id = "ProductTypeSub" name = "ProductTypeSub" class = "input-medium" ></select >
js
$("#ProductType").change(function () { changeDropdownList("#ProductType","#ProductTypeSub"); }); var changeDropdownList = function (source,target) { if ($(source).val() !== "") { $.ajax({ type: "POST",url: "/BillingBusiness/GetChildrenTypes",data: { parent: $(source).val() },success: function (data) { var innerHtml = "<option value=\"\">请选择</option>"; if (data!=null) { for (var item in data) { innerHtml += "<option value=\"" + item + "\">" + data[item] + "</option>"; } } $(target).html(innerHtml); var selectValue = ""; if (target == "ProductType") { selectValue = config.ProductType; } if (selectValue !== "") { $(target).val(selectValue); } } }); } else { $(target).html("<option value=\"\">请选择</option>"); } };//changeDropdownList() END
FileResult应用实例
导出excel。
controller
//导出业务帐 [ HttpGet ] public FileResult ExportBusinessAccount (BusinessAccountParam param) { param .ClientNo = "tst" ; var result = client .ExportBusinessAccount ( param); if (! result .Success ) { JsError (result . Message); } return File ( result. Result .StreamBytes,"application/ms-excel","BusinessAccountList.xls" ); //第一个参数为二进制字节流 //返回zip的话 return File ( result. Result .StreamBytes,"application/octet-stream","BusinessAccountList.zip" ); }
js
$ ( "#exportExcel" ).click ( function () { $ ("#frmSearch" ). attr( "action","ExportBusinessAccount" ); $ ("#frmSearch" ). submit(); });