现在的位置: 首页 > 综合 > 正文

MVC3学习~

2013年09月08日 ⁄ 综合 ⁄ 共 2431字 ⁄ 字号 评论关闭

路由设置:

/{controller}/{action}/{id},其中id是可选的

这样增加个修改参数:

/{controller}/{action}/{type}/{id},其中,type和id都是可选的。

 

1、@Html.ActionLink(item.Name, "Details", new { Id = item.Id }, new { @target = "_blank" })

item.Name:显示value值

Details:跳转到Details下

item.Id:跳转后传入的值Id

@target :跳转方式

2、@Url.Action("Details", new { Id = item.Id })

Details:跳转到的页面

item.Id:传入参数

如需传入多个参数值的话,@Url.Action("Details", new { Id1= item.Id1 },new { Id2 = item.Id2 })

3、两不同文件夹页面间跳转

@{Html.RenderAction("RankList", "Products");}

从当前文件夹的文件下跳转到Products文件夹下的RankList

若需要带参数则

@Html.ActionLink(item2.Name.CutString(15), "Details", "Articles", new { id = item2.Id }, null)

4、需对一个文本框进行限制字数

@Html.TextBoxFor(m => m.Name, new { @maxlength = "30" })

5、按钮事件写法

<input type="submit" name="login_submit" id="login_submit" value="" class="sub_static" />

 public ActionResult LogOn()
        {
            return View();
        }

//-----------------------这里注意,上面那个没有HttpPost,下面这个代表你点了以后回传到该事件
        [HttpPost]
        public virtual ActionResult LogOn(LogOnModel item)
        {
            bool isPower = false;
            string ip = Request.UserHostAddress;
            LogOn();//-------------注意,这里一定要写
            if (!ModelState.IsValid)
            {
                return View(item);
            }
            var item1 = _UsersBLL.Get(item.Name);
            if (item1 != null)
            {
                if (_UsersBLL.CheckUser(item1.Id, item.Password))
                {
                    FormsAuthentication.SetAuthCookie(item1.Id.ToString(), item.RememberMe);
                    isPower = item1.IsPower.HasValue? (item1.IsPower.Value? true:false):false;
                    Logs log = new Logs { Id = Guid.NewGuid(), CreateTime = DateTime.Now, UserId = item1.Id, Name = "用户登录", Content = item1.Name + "用户登录成功", IP = ip };
                    _LogsBLL.Save(null, ref log);
                    if (isPower)
                    {
                        return Redirect("/Admin");
                    }
                    else
                    {
                        if (Request["ReturnUrl"] != null)
                        {
                            return Redirect(Request["ReturnUrl"]);
                        }
                        else
                        {
                            return Redirect("/");
                        }
                    }                   
                }
                else
                {
                    Logs log = new Logs { Id = Guid.NewGuid(), CreateTime = DateTime.Now, UserId = null, Name = "用户登录", Content = "<span style='color:red'>失败的用户登录尝试。</span>", IP = ip };
                    _LogsBLL.Save(null, ref log);
                    ModelState.AddModelError("", "用户名不存在或密码错误!");
                }
            }
            else
            {
                Logs log = new Logs { Id = Guid.NewGuid(), CreateTime = DateTime.Now, UserId = null, Name = "用户登录", Content = "<span style='color:red'>失败的用户登录尝试。</span>", IP = ip };
                _LogsBLL.Save(null, ref log);
                ModelState.AddModelError("", "用户名不存在或密码错误!");
            }
            return View(item);
        }

抱歉!评论已关闭.