zoukankan      html  css  js  c++  java
  • ASP.NET MVC3开发中遇到问题以及解决方法

    在刚学MVC3,在学习过程中遇到了很多的问题,现在把已遇到问题总结出来,以后陆续更新。方便和我一样的新手。。

    1.手写Model类,EF执行错误找不到表对象。

    [TableAttribute("ProductEntity")]
    public class ProductEntity{}

    2.加载不同的Layout,在_ViewStart.cshtml中添加逻辑

    @{if (Request.Url.AbsoluteUri.Contains("Manage"))
    {
    Layout = "~/Views/Shared/_MLayout.cshtml";
    }else{
    Layout = "~/Views/Shared/_LayoutLogin.cshtml";
    }
    }

    3.图片image设置动态url
    a.Detail/Creat/Edit页面:

    @model TaiQiu.Models.ProductEntity
    <img id="preview" src="@Html.DisplayFor(model => model.PicUrl)"/>

    b.List页面:

    @model IEnumerable<TaiQiu.Models.ProductEntity>   
    @foreach (var item in Model)
    {
    <img src="@item.PicUrl" alt="@item.Title"/>
    }

    4.用户登录/权限

    //验证用户成功后,将用户写入cookie
    System.Web.Security.FormsAuthentication.SetAuthCookie(_user, false);
    //后台Controller中添加Authorize,如果可以配置Users/Role
    [Authorize(Users/Role = 允许账号/角色)]
    public class ManageController : Controller{}

    配置文件中其中Form验证

       <authentication mode="Forms">
    <forms loginUrl="~/Login/" timeout="2880" />
    </authentication>

    5.IIS6配置MVC3

    找不到 System.Web.Razor,System.Web.MVC 等。需要把开发环境下对应的dll复制到服务器bin文件下

    6.View中控件样式设置

    @Html.TextAreaFor(model => model.Infor, new { style = "800px;height:400px" })
    或者
    @Html.TextAreaFor(model => model.Infor, new { @class=样式名})

    7.TextArea设置Rows,Columns(第2个参数为rows,第3个参数为columns)

    @Html.TextAreaFor(model => model.FileInfo,5,5, new { style="300px;height:100px;"})

    8.文件上传,注意加粗红色部分

    View代码:

    @using (Html.BeginForm("actionName", "cotrollerName", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
    <input type="file" name="FilePath" id="FilePath" />
    }

    Controller代码:

    HttpPostedFileBase file = Request.Files[0] as HttpPostedFileBase;
    if (file.FileName != "")
    {
    //code
    }

    9.foreach,使用ViewBag动态特性

    Controller代码:

    var recommendporduct = PE.ProductEntity.Where(a => a.Recommend).Take(5);
    ViewBag.rplist = recommendporduct;
    return View();

    View代码:(注意:使用ViewBag,不会有代码提示。

           @foreach (var item in ViewBag.rplist)
    {
    <div class="rught_cptxt2">
    <ul>
    <li class="rught_cpimg1"><a href="#">
    <img src="@item.PicUrl.Trim()" border="0" /></a></li>
    <li class="rught_cptxt1"><a href="#">@item.Title</a></li>
    </ul>
    </div>
    }

    10.DropDownList绑定
    Controller代码:

            //类别
    public SelectList GetSL()
    {//此处静态数据,可以使用EF读取数据库
    List<SelectListItem> list = new List<SelectListItem> {
    new SelectListItem(){Value="0",Text="新闻资讯"},
    new SelectListItem(){Value="1",Text="技术文章"}
    };
    return new SelectList(list, "Value", "Text");
    }
    public ActionResult Create()
    {
    ViewBag.ddl = GetSL();
    return View();
    }

    View代码:

    //引号中对应ViewBag中的值
    @Html.DropDownList("ddl")
    或者使用强类型(将GetSL()静态方法放在类中,直接调用。)
    @Html.DropDownListFor(model => model.IsTechnology, GetSL(), "请选择")

     11.查询

    a.EF4.1中支持使用sql:

    DbSet<BlogMaster>set= context.Set<BlogMaster>();
    List
    <BlogMaster> list =set.SqlQuery("select *from BlogMaster where UserId='3'"
    ).ToList();

    b.使用linq to entity framework:
    DemoDBEntities context =new DemoDBEntities();
    DbSet
    <BlogMaster>set= context.Set<BlogMaster>();
    var result 
    = from u inset.ToList()
    where u.UserID ==3

    select u;

    c.使用labmda查询:
    var list =set.Where(o => o.UserID ==3);
    var listGroup 
    =set.GroupBy(o => o.UserID);

    12.输出html标签

    传参

    @Html.Action("Header","Common",new RouteValueDictionary{{"controllerName",controllerName }})

    @Html.Raw(内容)
  • 相关阅读:
    leetcode 122. Best Time to Buy and Sell Stock II
    leetcode 121. Best Time to Buy and Sell Stock
    python 集合(set)和字典(dictionary)的用法解析
    leetcode 53. Maximum Subarray
    leetcode 202. Happy Number
    leetcode 136.Single Number
    leetcode 703. Kth Largest Element in a Stream & c++ priority_queue & minHeap/maxHeap
    [leetcode]1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree
    正则表达式
    十种排序算法
  • 原文地址:https://www.cnblogs.com/LYunF/p/2944088.html
Copyright © 2011-2022 走看看