Controller 獲得 View的幾個方法
有稍微幾種語法
第一種是常用的ajax
$.ajax({
type:"POST",
data:data //data可以先自行包東西進去 通常是包個json進去
//data:{ data:"data" , bata:"bata" }
//Url:@Url.Action("Action" , "Controller"), //Razor
//Url:"Controller/Action", //MVC
Url:"URL",
});
大致上是這樣 還可以用error 或者success 之類的東西測試成功失敗
第二種是直接用OnClick
裡面也算是透過Razor的部分
<button ... onclick="window.open('@Url.Action("Action" , "Controller" ,
new { target = "_blank" , count = 1})')">
後面的new {...} 裡面的值會自動轉換成?...
也就是變成 ?count=1&...的型態
第三種是MVC的功能
@using (Html.BeginForm("Action" , "Controller")){
//是一個Razor提供的方法 用Submit可以把目前頁面View的Model回傳至後端
}
以上三種應該都能用Public ActionResult Action(){return View();}接
而由於MVC系列的Model會自動對上,因此後端地接收回傳值甚至可以寫個Model去接他
只要裡面屬性的名字跟回傳值有對上就好
如data:{data:"999"}
那Model如果裡面有 data的話 就可以呼叫 Model.data 得到會是999
補充方法
1.
/* HTML section */
<html>
..........
<button type="button" id="BtnSend" value="OK" /></button>
..........
</html>
/* Script section */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).on('click','#BtnSend',function() {
alert('clicked!');
});
</script>
2.
/* 一般 HTML 寫法 */
<html>
..........
<form id="FormId">
<input type ="submit" value="確認" /> //提交
<input type ="reset" value="重置" /> //重置
</form>
..........
</html>
/* 使用 Javascript 寫法 */
<html>
..........
<form id="FormId">
<button id="BtnId">確認</button>
</form>
..........
</html>
<script type="text/javascript">
document.getElementById("BtnId").addEventListener("click", function () {
var form = document.getElementById("FormId");
form.submit();
});
</script>參考:https://ithelp.ithome.com.tw/articles/10197740
https://ithelp.ithome.com.tw/questions/10196302
https://linmasaki09.blogspot.com/2013/09/button.html
留言
張貼留言