Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript仅使用索引操作MVC4_Javascript_Jquery_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

JavaScript仅使用索引操作MVC4

JavaScript仅使用索引操作MVC4,javascript,jquery,asp.net-mvc,asp.net-mvc-4,Javascript,Jquery,Asp.net Mvc,Asp.net Mvc 4,我正在尝试开发一个Mvc4应用程序,它有一个javascript来显示随机提示。但这些javascript只在通过索引操作调用时才起作用。我想用javascript处理其他操作。请帮忙做这件事 这是我的控制器 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace JQuaryError.Controllers {

我正在尝试开发一个Mvc4应用程序,它有一个javascript来显示随机提示。但这些javascript只在通过索引操作调用时才起作用。我想用javascript处理其他操作。请帮忙做这件事

这是我的控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace JQuaryError.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Test()
        {
            return View();
        }
        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}
下面是使用javascript的视图

 @{
    ViewBag.Title = "Home Page";
}
@section featured {
     <script type="text/javascript" src="Scripts/jquery.js"></script>

  <script type="text/javascript">

      this.randomtip = function () {

          var pause = 3000; // define the pause for each tip (in milliseconds) Feel free to make the pause longer so users can have time to read the tips :)
          var length = $("#tips li").length;
          var temp = -1;

          this.getRan = function () {
              // get the random number
              var ran = Math.floor(Math.random() * length) + 1;
              return ran;
          };
          this.show = function () {
              var ran = getRan();
              // to avoid repeating tips we need to check 
              while (ran == temp) {
                  ran = getRan();
              };
              temp = ran;
              $("#tips li").hide();
              $("#tips li:nth-child(" + ran + ")").fadeIn("fast");
          };
          // initiate the script and also set an interval
          show(); setInterval(show, pause);

      };

      $(document).ready(function () {
          randomtip();
      });
</script>


}
<h3>We suggest the following:</h3>
<ul id="tips">
        <li>... if you want to become a better coder you need to eat your vegetables?</li>
        <li>... it takes more time to code a web page then to make a pizza?</li>
        <li>... you should validate your code?</li>
        <li>... jQuery is your friend? For real!</li>
        <li>... no matter what some people claim, you can't learn CSS in 3 hours?</li>
    </ul>
@{
ViewBag.Title=“主页”;
}
@特色栏目{
this.randomtip=函数(){
var pause=3000;//定义每个提示的暂停时间(以毫秒为单位),可以随意延长暂停时间,以便用户有时间阅读提示:)
变量长度=$(“#tips li”)。长度;
var-temp=-1;
this.getRan=函数(){
//获取随机数
var ran=Math.floor(Math.random()*长度)+1;
回程跑;
};
this.show=函数(){
var-ran=getRan();
//为了避免重复提示,我们需要检查
while(ran==temp){
ran=getRan();
};
温度=ran;
$(“#tips li”).hide();
$(“#tips li:nth child(“+ran+”)).fadeIn(“fast”);
};
//启动脚本并设置间隔
show();设置间隔(显示、暂停);
};
$(文档).ready(函数(){
随机提示();
});
}
我们建议如下:
  • 。。。如果你想成为一名更好的编码员,你需要吃蔬菜吗
  • 。。。编写网页代码比制作比萨饼要花更多的时间
  • 。。。你应该验证你的代码吗
  • 。。。jQuery是你的朋友吗?真的
  • 。。。不管有些人怎么说,你都不能在3小时内学会CSS

如果您只在一个视图
.cshtml
文件中包含JavaScript代码,则只会为该操作的视图加载它。您可能知道,当控制器执行
returnview()时
在每个操作中,它将返回与ActionResult方法同名的视图。由于MVC是无状态的,当您从应用程序的一个页面移动到另一个页面时,不会传递任何状态信息,因此当您在视图之间移动时,不会保留任何脚本/css/任何其他内容。这意味着对于每个操作,您需要将脚本包含在相应的
.cshtml
文件中

也就是说,如果您知道要将其包含在每个视图中,则可以创建一个布局视图,供所有视图使用。这样,就可以在布局中的所有视图上使用JavaScript脚本,而不必在每个视图中重复