MVC项目日常问题解决记录

问题1: Html.RenderPartial 报错(找不到创建的控件)

答案: 如果你的partial(本例中也就是Header.ascx)是在当前请求的controller下(也就是位于目录/Views/nameController下)或共享目录下(也就是/Views/Shared下),那么你只要把后缀.ascx去掉就行了,也就是把你的第26行换成<% Html.RenderPartial("Header.ascx"); %>。

如果你的partial位于其它位置,那么你需要引用全虚拟目录,目录依你项目而定,形式如下:
<%: Html.Partial("~/Views/Shared/Partials/MyOtherPartial.ascx") %>
注意,这个时候需要.ascx后缀。

 

问题2:如何在网站中排列图片(自动换行排列)

答案:设置一个div(宽度),里层有 <ul> 循环多个<Li width="设定宽度">

            <div class="community_pic">
                <ul>
                    <asp:Repeater ID="rptActiveUser" runat="server">
                        <ItemTemplate>
                            <li><a href="/account/profile.html?<%#Eval("CreateUserId")%>" title="<%#Eval("UserNickName") %>">
                                <img width="41" height="41" src="<%#Wordoor.BLL.FilePath.GetUserPortraitUrl(Eval("UserPortraitPath")) %>"
                                    title="" alt="<%#Eval("UserNickName") %>"></a></li></ItemTemplate>
                    </asp:Repeater>
                </ul>
            </div>

 

问题3: 编译报错,提示没有引用某个命名空间

 

答案:需要在web.config中添加

<compilation debug="true">

      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
      </assemblies>
   </compilation>   

问题4:sql写法

答案:

SQL: select top 10 V_RankingLevel v from V_RankingLevel Wherec.levelID=@level order by AccuracyRate desc

ESQL:

Select value c from V_RankingLevel as c where c.levelID=@level order by c.AccuracyRate skip 0 limit 10

 

执行:

return _context.CreateQuery<V_RankingLevel>(strsql, new ObjectParameter("level",model.CurrentLevel)).ToList();

 

问题5:页面控件样式设置

 <%=Html.TextBox("hello","内容", new { Style="width:200px;"})%>

 

问题6:页面控件样式设置

 <%=Html.TextBox("idname","显示",})%>

 <input type="submit" value="submit" />

 [HttpPost]

public ActionResult Index(int id, FormCollection collection )

       {
            return View();
        }

Contorller中取得页面数据:Request.Form("idname") 或者 collection["idname"]都可以取得前台值

 

问题7:页面内容判断:dropdownlist,内容验证

 var selectlist = default(SelectList);

selectlist =new SelectList(categories, "ID""Name");这个写法用于前台给dropdowlist赋值

ViewData["LevelList"] = selectlist

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

<% SelectList categories = ViewData["Categories"as SelectList; %>

<%= Html.TextBox("Title"%></dd>
<dd><%= Html.ValidationMessage("TitleValidator"%></dd>

if (String.IsNullOrEmpty(Request.Form["Title"]))
             {
                    ViewData.ModelState.AddModelError("TitleValidator","公告标题不能为空!");
                }

        public ActionResult Submit(SubmitResultModel resultModel)
        {
            ActionResult result = null;

问题8:前台用模型作为参数

    public class SubmitResultModel
    {
        public int ActId { get; set; }
        public int Count { get; set; }
        public int Score { get; set; }
        public string Data { get; set; }
    }

            try
            {
                //TODO: submit result into database and check finish or not.
                result = this.Json(new { success = true });
            }
            catch (Exception ex)
            {
                result = this.Json(new { failture = true, msg = ex.Message });
            }

            return result;
        }
    }

--前台提交的时候,保证参数名称和类中名称一致,并且参数名和后台参数名一致就可以了

        function AJAX(resultobj) {
            var resultModel = "ActId=" + ActivityModel.actId + "&Count=" + ActivityModel.count + "&Score=" + ActivityModel.Score + "&Data=" + ActivityModel.Data;
            $.ajax({
                type: "post",
                url: "/Contorller/Action",
                data: resultModel,
                dataType: 'json',
                success: function (data) {
                    if (data.success == true) {

                    }
                    else {
                        alert("失败,请重新操作");
                    }
                },
                error: function (e) {
                    alert(e);
                }
            });
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值