MVC3.0----整理之一

1.ViewBag对象是动态解析对象

@{
     ViewBag.Title = "Insert";
 }

不仅可以把单个的对象绑定到 ViewBag上,还可以直接绑定List

 ViewBag.Category = db.Category.ToList(); 下面直接生成一个select

 <select name="search-sort" id="sel_category">
        <option value="">--请选择--</option>
        @foreach (var item in ViewBag.Category)
        {
            <option value="@item.Cid">@item.CategoryName</option>
        }
     </select>

2.画面实体类对象传递

@model BlogMVC.Models.DataInfo

使用方式 @Model.Title,其中 Title 是 DataInfo类的字段

 

3.画面直接输出html源代码

@(new HtmlString(Server.UrlDecode(Model.Content)))

 

4.jquery的使用 之 单个值传递

string json = "{\"b\": \"" + result + "\"}"; return json;

画面上调用:

 

 $.ajax({
            type: "POST",
            url: "/AjaxAction/InsertBlog",
            data: { title: title, tag: tag, category: category, mcontent: mcontent },
            dataType: "text",
            async: true,
            beforeSend: function () {
                //alert("beforeSend");
            },
            success: function (d) {
                // alert("succeed");
                var jsons = eval("(" + d + ")");
                if (jsons.b > 0) {
                    alert("提交成功");
                } else {
                    alert("提交失败");
                }
            }
        });

 

5.jquery的使用 之 datatable 数据集画面展示

      public string SearchAction(string keywords,int pageIndex=1)
        {

            string json = "";

            SqlParameter[]parms = new SqlParameter[]{

                new SqlParameter("@keywords",keywords),
                new SqlParameter("@pageIndex",pageIndex),
                new SqlParameter("@pageSize",10)
            };

          DataTable dt =  SqlHelper.GetTable("usp_getDataManager", parms);

          Dictionary<string, object> dict = new Dictionary<string, object>();

          if (dt!=null&&dt.Rows.Count > 0)
          {
              int count = (int)SqlHelper.ExecuteScalar("usp_getDataCountManager", new SqlParameter[] { new SqlParameter("@keywords", keywords) });

              string jsonData = JsonConvert.SerializeObject(dt);
              dict.Add("jsonData", jsonData);
              dict.Add("dataCount", count);
              json = JsonConvert.SerializeObject(dict);
          }
          else
          {
              string jsonData = JsonConvert.SerializeObject(dt);
              dict.Add("jsonData", "");
              dict.Add("dataCount", -1);
              json = JsonConvert.SerializeObject(dict);
          }
           
            return json;
        }

 

画面上的调用:

function ajaxQueryFunc(keywords, pageindex) {
        $.ajax({
            type: "post",
            data: { keywords: keywords, pageindex: pageindex, type: "sc" },
            url: "/AjaxAction/Search",
            dataType: "text",
            beforeSend: function () {
                //从tr 第2行到末尾的都要删除
                var headr = $("#tab_data tr").first();
                $("#tab_data").children().remove();
                $("#tab_data").append(headr);
            },
            success: function (d) {

                var jsons = eval("(" + d + ")");
                var dataCount = jsons["dataCount"];
                if (dataCount > 0) {
                    showPagingDiv(dataCount, pageindex);
                    var jsonData = jsons["jsonData"];
                    var data = eval(jsonData);
                    var strTr = "";
                    for (cc in data) {
                        if (typeof (data[cc]) == 'object') {
                            strTr = strTr + "<tr> ";
                            strTr = strTr + "<td class='tc'><input name='ck_dataid' value='" + data[cc]['DataId'] + "' type='checkbox'></td>";
                            strTr = strTr + "<td class='tc'><input class='common-input sort-input' name='ord[]' value='" + data[cc]['Sequence'] + "' type='text'></td>";
                            strTr = strTr + "<td>" + data[cc]['DataId'] + "</td>";

                            strTr = strTr + "<td><a target='_blank' href='/Blog/Detail/" + data[cc]['DataId'] + "' title='" + data[cc]['Title'] + "'>" + data[cc]['Title'] + "</a></td>";

                            strTr = strTr + "<td>" + data[cc]['Click'] + "</td>";
                            strTr = strTr + "<td>" + data[cc]['Author'] + "</td>";

                            strTr = strTr + "<td>" + "2014-03-15 21:11:01" + "</td>";
                            strTr = strTr + "<td>" + data[cc]['Tags'] + "</td>";
                            strTr = strTr + "<td><span class='sp_link' onclick=\"sp_link_edit_click(this);\" >修改排名</span> &nbsp;  <span class='sp_link'  onclick=\"sp_link_delete_click(this);\" >删除</span> &nbsp; <div class='div_mess'></div></td>";
                            strTr = strTr + "</tr> ";
                        }
                    }
                    $("#tab_data").append(strTr);

                }
                else {
                    alert("没有检索到数据");
                }
            }
        });

    }

 

posted @ 2015-07-02 09:51  兴想事成  阅读(229)  评论(0编辑  收藏  举报