zoukankan      html  css  js  c++  java
  • mvc 总结

                @Html.DropDownList("sex", new SelectList(Enum.GetValues(typeof(enumSex))), new { @class = "form-control",data_bind="value: Sex"})

    1、mvc htmlhelper 中加入data-属性,应改为data_

    This problem has been addressed in ASP.Net MVC 3. They now automatically convert underscores in html attribute properties to dashes. They got lucky on this one, as underscores are not legal in html attributes, so MVC can confidently imply that you'd like a dash when you use an underscore.

    For example:

    @Html.TextBoxFor(vm => vm.City, new { data_bind = "foo" })
    

      

    will render this in MVC 3:

    <input data-bind="foo" id="City" name="City" type="text" value="" />
    

      

    If you're still using an older version of MVC, you can mimic what MVC 3 is doing by creating this static method that I borrowed from MVC3's source code:

    public class Foo {
        public static RouteValueDictionary AnonymousObjectToHtmlAttributes(object htmlAttributes) {
            RouteValueDictionary result = new RouteValueDictionary();
            if (htmlAttributes != null) {
                foreach (System.ComponentModel.PropertyDescriptor property in System.ComponentModel.TypeDescriptor.GetProperties(htmlAttributes)) {
                    result.Add(property.Name.Replace('_', '-'), property.GetValue(htmlAttributes));
                }
            }
            return result;
        }
    }

    And then you can use it like this:

    <%: Html.TextBoxFor(vm => vm.City, Foo.AnonymousObjectToHtmlAttributes(new { data_bind = "foo" })) %>
    

      

    and this will render the correct data-* attribute:

    <input data-bind="foo" id="City" name="City" type="text" value="" />



    2、枚举绑定

                @Html.DropDownList("sex", new SelectList(Enum.GetValues(typeof(enumSex))), new { @class = "form-control",data_bind="value: Sex"})
                    @Html.DropDownListFor(model => model.DepartmentType, new SelectList(Enum.GetValues(typeof(enumDepartmentType))))

     但是上两种方法只有text,没有value,所以有个优化方案如下

             knockout select绑定,选中默认值                     

    假设我们有一个enum:

    public enum Role{
    User = 0,
    Admin = 1024
    }

    因为enum本身并没有IEnumerable接口,所以不能直接使用new SelectList(Role);来将之填充DropDownList。

    但是我们可以写一个静态方法将之转为IEnumerable。

    public class EnumExt{
        static public List<ListItem> ToListItem<T>(){
            List<ListItem> li = new List<ListItem>();
            foreach (int s in Enum.GetValues(typeof(T))){
                li.Add(new ListItem{
                    Value = s.ToString(),
                    Text = Enum.GetName(typeof (T), s)
                }
                );
            }
            return li;
        }
    }

    View文件中我们加入以下helper:

      <%=Html.DropDownList("enumlist") %>

    然后我们在Controller的action中写如下绑定即可

      public ActionResult Index()
    
    {
    
    ViewData["enumlist"] = new SelectList(EnumExt.ToListItem<Role>(),"value","text");
    
    return View();
    
    }
    这样我们就可以实现将Enum绑定在DropDownList了
    前提还要有一个ListItem类
      public class ListItem
        {
            public string Value { get; set; }
            public string Text { get; set; }
        }

     3.WebImage使用

    a、使用WebApi输出图片

     public HttpResponseMessage Get(string imageName, int width, int height)
        {
            Image img = GetImage(imageName, width, height);
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
            result.Content = new ByteArrayContent(ms.ToArray());
            result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
            return result;
        }

     或者

    using System.IO;
    /// <summary>
    /// WebApi返回图片
    /// </summary>
    public HttpResponseMessage GetQrCode()
    {
        var imgPath = @"D:ITdosComImagesitdos.jpg";
        //从图片中读取byte
        var imgByte = File.ReadAllBytes(imgPath);
        //从图片中读取流
        var imgStream = new MemoryStream(File.ReadAllBytes(imgPath));
        var resp = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new ByteArrayContent(imgByte)
            //或者
            //Content = new StreamContent(stream)
        };
        resp.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
        return resp;
    }
    /// <summary>
    /// WebApi返回json数据
    /// </summary>
    public HttpResponseMessage GetQrCode()
    {
        var jsonStr = "{"IsSuccess":true,"Data":"www.itdos.com"}";
        var result = new HttpResponseMessage(HttpStatusCode.OK)
                        {
                            Content = new StringContent(jsonStr, Encoding.UTF8, "text/json")
                        };
        return result;
    }
    /// <summary>
    /// WebApi返回字符串
    /// </summary>
    public HttpResponseMessage GetQrCode()
    {
        var str = "IT大师www.itdos.com";
        var result = new HttpResponseMessage(HttpStatusCode.OK)
                        {
                            Content = new StringContent(str, Encoding.UTF8, "text/plain")
                        };
        return result;
    }

     b、再mvc中返回图片

    using System.IO;
    /// <summary>
    /// WebApi返回图片
    /// </summary>
    public HttpResponseMessage GetQrCode()
    {
        var imgPath = @"D:ITdosComImagesitdos.jpg";
        //从图片中读取byte
        var imgByte = File.ReadAllBytes(imgPath);
        //从图片中读取流
        var imgStream = new MemoryStream(File.ReadAllBytes(imgPath));
        var resp = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new ByteArrayContent(imgByte)
            //或者
            //Content = new StreamContent(stream)
        };
        resp.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
        return resp;
    }
    /// <summary>
    /// WebApi返回json数据
    /// </summary>
    public HttpResponseMessage GetQrCode()
    {
        var jsonStr = "{"IsSuccess":true,"Data":"www.itdos.com"}";
        var result = new HttpResponseMessage(HttpStatusCode.OK)
                        {
                            Content = new StringContent(jsonStr, Encoding.UTF8, "text/json")
                        };
        return result;
    }
    /// <summary>
    /// WebApi返回字符串
    /// </summary>
    public HttpResponseMessage GetQrCode()
    {
        var str = "IT大师www.itdos.com";
        var result = new HttpResponseMessage(HttpStatusCode.OK)
                        {
                            Content = new StringContent(str, Encoding.UTF8, "text/plain")
                        };
        return result;
    }

    垫底

  • 相关阅读:
    【测试】模拟一个全表扫描的sql,对其进行优化走索引,并且将执行计划稳定到baseLine。
    【练习】手工生成awr报告
    【测试】数据文件的删除恢复
    【练习】行迁移和行链接
    织梦栏目列表页第一个文章与其他文章不同样式
    织梦联动筛选【单选版】-支持手机站使用
    织梦联动枚举字段无二级时去掉多余下拉
    织梦联动类型地区联动三级修复以及省份-市级-地区分开+高亮
    织梦联动枚举字段添加一级分类如果超过132个自动变成二级修复教程
    织梦后台自定义表单联动地区显示为数字的真正解决方法
  • 原文地址:https://www.cnblogs.com/longyi/p/5677329.html
Copyright © 2011-2022 走看看