.NET MVC学习笔记三
1、c#去掉guid中間的橫杠
string testGuidStr= Guid.NewGuid().ToString("N");
2、MVC頁面刪除前提示
2.1 在頁面加入如下代碼
<script type="text/javascript">
function confirmDelete(delUrl) {
if (confirm("您確定要刪除用戶嗎?")) {
document.location = delUrl;}}
</script>
2.2 點擊刪除的URL鏈接代碼
3、在瀏覽器中新彈出窗口
3.1 加入scripts代碼
@section scripts {
<scripttype="text/javascript">
function openwin() {
window.open("../Book/BookTypeInfo.html", "newwindow", "height=100, width=400, toolbar =no, menubar=no, scrollbars=no, resizable=no, location=no, status=no") }}
</script>
3.2 鏈接
<ahref="#"onclick="openwin()">打開一個窗口</a>
3.3 給新彈出的窗口,加一個關閉按鈕
<form>
<inputtype='button'value='關閉'onclick='window.close()'>
</form>
4、操作MySQL進行模糊查詢
4.1 主要知識點
使用instr(要查詢的字段,?要匹配的參數)函數,加參數
4.2 參考代碼
MySqlConnection myconn = null;
MySqlCommand mycom = null;
MySqlDataReader myreader = null;
myconn = newMySqlConnection(DbConnction.connectionString);
mycom = newMySqlCommand("select * from 要查詢的表 where instr(要查詢的字段,?參數) ", myconn);
mycom.Parameters.Add("@參數", MySqlDbType.VarChar).Value = “要模糊查詢的內容”;
myconn.Open();
myreader = mycom.ExecuteReader();
while (myreader.Read()){讀數據庫中的內容}
if (myreader != null) { myreader.Close(); }
if (myconn != null) { myconn.Close(); }
5、查詢發布文章最多的前10名用戶姓名
5.1 思路
獲得發布文章的用戶組,然后統計用戶組中用戶個數,排序,找出前10名用戶。
5.2 參考語句
以MySQL方法為例:
select用戶姓名 from(select count(用戶姓名)as UserNum,用戶姓名 from 表 group by 用戶姓名) UserNumTable order by UserNum desc limit 0,10
6、 mvc操作json
6.1 前端代碼
<span id="_content" ></span>
<script type="text/javascript">
function upclick(o) {
var obj = document.getElementById("輸入框的Id值").value;
$.ajax({
url: "/提交的請求地址/參數?ran=" + Math.random(),
type: "GET",
dataType: "json",
data: {參數: obj},
success: function (data) {
$("#_content").html(data);
}, }) }
</script>
6.2 后端代碼
放到 ViewAction里
var res = new JsonResult();
res.Data = "請輸入要查詢的內容";
//允許使用GET方式獲取,否則用GET獲取是會報錯。
res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return res;
7、view前端輸入框JavaScript校驗
7.1以輸入框<input type="text" name="test" id=" test " class=" test "/>為例
<form action=" " method="post">
<input type="text" name="test" id=" test " class=" test "/>
<input type="submit" value="提交" onclick="return check(this.form)"/>
</form>
7.2提示顯示 :<span id="_content" ></span>
7.3校驗內容: 輸入框不能為空值,輸入的字符不能超過30
<script type="text/javascript">
function check(form){
var obj = document.getElementById("test ").value;
if (form.findText.value == '') {
$("#_content").html("輸入不能為空");
form.findText.focus();
return false;
} else if (obj.length > 30) {
$("#_content").html("輸入的字符最多為30");
form.findText.focus();
return false;
} else
{
return true;
}
}
</script>
8、RouteLink使用
<li>
@Html.RouteLink(linkText: "幫助中心", routeName: "Default", routeValues: new { controller = "HelpCenter", action = "Index" }, htmlAttributes: new { @onmouseover = "return ContentDivChange('1','6',7);" })
</li>
9、點擊button返回上一頁
<button onclick="window.location='@Request.UrlReferrer'">返回</button>
或
<input type="button" value="返回"onclick="javascript:history.go(-1);"/>
10、用Html.TextBoxFor設置默認值
@Html.TextBoxFor(m => m.模型, new { @Value = (Model==null) ? “默認值” : Model. 模型})
11、如何禁用Visual Studio 2013的Browser Link功能
11.1 方法一
11.2 方法二
12、jquery 實現鼠標點擊文本框清空文本內容,鼠標離開后,恢復內容
<script>
$(function () {
var text; // 全局變量用于保存文本框的內容
$("input:text").focus(function () {
text = $(this).val();
$(this).val("");
});
$("input:text").blur(function () {
$(this).val() != "" || $(this).val(text);
}); })
</script>
13、mvc帶參數跳轉頁面
return RedirectToAction("Action名","控制器名", new { 參數= 參數值});
14、jQuery圖片輪播的實現
14.1 html代碼
<div id="scrollPics">
<ul class="slider" >
<li><img src="images/ads/1.gif"/></li>
<li><img src="images/ads/2.gif"/></li>
<li><img src="images/ads/3.gif"/></li>
<li><img src="images/ads/4.gif"/></li>
<li><img src="images/ads/5.gif"/></li>
</ul>
<ul class="num" >
<li class="on">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
</div>
14.2 css代碼
#scrollPics{
height: 150px;
width: 100%;
margin-bottom: 10px;
overflow: hidden;
position:relative;
}
.num{
position:absolute;
right:5px;
bottom:5px;
}
#scrollPics .num li{
float: left;
color: #FF7300;
text-align: center;
line-height: 16px;
width: 16px;
height: 16px;
cursor: pointer;
overflow: hidden;
margin: 3px 1px;
border: 1px solid #FF7300;
background-color: #fff;
}
#scrollPics .num li.on{
color: #fff;
line-height: 21px;
width: 21px;
height: 21px;
font-size: 16px;
margin: 0 1px;
border: 0;
background-color: #FF7300;
font-weight: bold;
}
用絕對定位設置列表 num 的位置,對 li 設置相關樣式,on 表示顯示圖片對應的數字列表中 li 的樣式類別
14.3 javascript代碼
//滾動廣告
var len = $(".num > li").length;
var index = 0; //圖片序號
var adTimer;
$(".num li").mouseover(function() {
index = $(".num li").index(this); //獲取鼠標懸浮 li 的index
showImg(index);
}).eq(0).mouseover();
//滑入停止動畫,滑出開始動畫.
$('#scrollPics').hover(function() {
clearInterval(adTimer);
}, function() {
adTimer = setInterval(function() {
showImg(index)
index++;
if (index == len) { //最后一張圖片之后,轉到第一張
index = 0;
}
}, 3000);
}).trigger("mouseleave");
function showImg(index) {
var adHeight = $("#scrollPics>ul>li:first").height();
$(".slider").stop(true, false).animate({
"marginTop": -adHeight * index + "px" //改變 marginTop 屬性的值達到輪播的效果
}, 1000);
$(".num li").removeClass("on")
.eq(index).addClass("on");
}