1
我是ASP.net MVC的新手。 頁輸出:如何在ASP.NET MVC中着色單詞
這是紅
這是藍
此格林
它不colored.I要進行着色。
我找不到故障。我在哪裏做錯誤?
這是我的控制器
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
public JsonResult RGBColor()
{
RGB color=new RGB();
return Json(color,JsonRequestBehavior.AllowGet);
}
}
這是我的模型
public class RGB
{
public string Red = "#FF0000";
public string Green = "#00FF00";
public string Blue = "#0000FF";
}
這是我的看法
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-
3.2.1.min.js">
$(document).ready(function() {
$.ajax({
type: "POST",
url: "/Home/RGBColor",
dataType: "json",
contentType: "application/json;charset=utf-8",
success: function (data) {
$(".red").css("color", data.Red);
$(".blue").css("color", data.Blue);
$(".green").css("color", data.Green);
}
})
})
</script>
</head>
<body>
<div>
<p class="red">This is Red</p>
<p class="blue">This is Blue</p>
<p class="green">This is Green</p>
</div>
你有沒有試着調試器,如果你簽入'RGBColor'也可能有助於增加'[Httppost]''以上RGBColor' –
你忘了包括'[ HttpPost]'over'public JsonResult RGBColor()'。如果未找到該方法,則CSS分配從未執行。 –
我增加了[HttpPost]但沒有改變。 – myvalley