MVC3學習~

路由設置:

/{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);
        }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章