zoukankan      html  css  js  c++  java
  • 【基础】一个简单的MVC实例及故障排除

    Controller:

     1  public ActionResult Index()
     2         {
     3             string setting = "ApplicationServices";
     4             var connString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings[setting];
     5             SqlConnection conn = new SqlConnection(connString.ConnectionString);
     6             SqlCommand cmd;
     7             SqlDataReader datar;
     8             string str_sql = "select * from T_category;";
     9             conn.Open();
    10             cmd = new SqlCommand(str_sql, conn);
    11             datar = cmd.ExecuteReader();
    12             List<string> list_categoery = new List<string>();
    13             while (datar.Read())
    14             {
    15                 list_categoery.Add(datar["category_name"].ToString());
    16             }
    17             conn.Close();
    18             ViewBag.Categories = list_categoery;
    19 
    20             str_sql = "select * from T_product;";
    21             conn.Open();
    22             cmd = new SqlCommand(str_sql, conn);
    23             datar = cmd.ExecuteReader();
    24             productModel pm = new productModel();
    25             while (datar.Read())
    26             {
    27                 pm.product_name = datar["Product_name"].ToString();
    28                 pm.product_description = datar["Product_description"].ToString();
    29                 pm.product_image = datar["Product_image"].ToString();
    30                 pm.product_document = datar["Product_document"].ToString();
    31             }
    32             conn.Close();
    33             return View(pm);
    34         }

    View:

    @model MvcApplication2.Models.productModel
    
    
    @{
        Layout = null;
    }
    
    <div id="categories">
                        <ul>
                            @foreach (var cate in (ViewBag.Categories) as IEnumerable<string>)
                                {
                                    <li>@cate</li>
                                }
                        </ul>
                    </div>
    
     <section id="isBIM_models">
                    <div>
                        <div id="Section_one">
                            <div id="One_product">
                                <div id="product_image">
                                    <img src="~/image/isBIM_product/@(Model.product_image).jpg"/>
                                </div>
                                <div id="product_description">
                                    <p>简介:<br />@Model.product_description</p>
                                </div>
                                <div class="clearfix"></div>
                                <div id="product_source">
                                   <a href="~/DLL/@(Model.product_name)_DLL_2015.dll">下载</a>
                                    <input type="button" id="downloadbutton"/>
                                   @*<a href="~/Home/download">Click to get file</a>*@
                                    
                                </div>
                            </div>
                        </div>
                    </div>
                </section>

     (view中只列出了绑定模型字段的部分)

    Model:

     public class productModel
        {
            public productModel() { }
            public int product_id { get; set; }
            public string product_name { get; set; }
            public string product_description { get; set; }
            public string product_image { get; set; }
            public string product_document { get; set; }
    
        }


    如果运行时遇到这样的报错:Microsoft.CSharp.RuntimeBinder.RuntimeBinderException

    则在Assemblyinfo.cs中添加一句:

    [assembly: InternalsVisibleTo("MvcApplication2.Controllers")]
  • 相关阅读:
    Codeforces369E Valera and Queries
    Codeforces369C Valera and Elections
    笔记 navmesh
    笔记 fastbuild
    C++ 遍历某个文件夹下所有文件
    ACM 已结束
    2018 “百度之星”程序设计大赛
    2018 Multi-University Training Contest 1 1002 /hdu6299 贪心 1007 /hdu6304 找规律
    2018 Multi-University Training Contest 5 1008 / hdu6357 Hills And Valleys LCS,思维
    牛客网暑期ACM多校训练营(第二场)G transform 思维,二分
  • 原文地址:https://www.cnblogs.com/lovecsharp094/p/5511079.html
Copyright © 2011-2022 走看看