【问题标题】:ASP.NET MVC- functionalities of a view doesn't work unless I access it directly除非我直接访问它,否则 ASP.NET MVC- 视图的功能不起作用
【发布时间】:2017-08-23 04:00:42
【问题描述】:

我有一个带有侧边栏的视图,其中包含文件列表,部分视图包含一个文本区域。我想在用户单击文件后显示文件的内容(通过刷新部分视图)。这是我正在使用的脚本:

 <script>
     $(document).ready(function () {
         $('.filelist').on('click', function (e) {
             var filePath = $(this).attr('value');    //value is attribute set in Html
             $('#partial').load('DevelopmentPartial', { path1: filePath });
         });
     });
</script>

它一直正常工作,直到昨天,我在重定向到此视图时开始收到此错误,但如果直接访问它则不会:

请求的内容似乎是脚本,不会被 静态文件处理程序

我能够摆脱错误,但现在除非我访问 直接查看,但如果我从另一个操作方法重定向到它,它不会!

这是我的控制器:

 StudentsCodes modelSC;

    public DevelopmentController()
    {
        m4 = new MicroG4();
        modelSC = new StudentsCodes();
    }
    public ActionResult Index()
    {
        modelSC.Student = (Student)CurrentUser;
        var user = UserManager.FindById(((Student)CurrentUser).InstructorID);
        modelSC.Instructor =(Instructor) user;
        //modelSC.path = "~/Content/" + CurrentUser.UserName + "/CompilerProject/src/Driver.java";
         return View(modelSC);
    }
    public PartialViewResult DevelopmentPartial(string path1)
    {
        modelSC.Student = (Student)CurrentUser;
        var user = UserManager.FindById(((Student)CurrentUser).InstructorID);
        modelSC.Instructor = (Instructor)user;
        modelSC.path = path1;
        modelSC.code = "";
        if (modelSC.path == null)
        {
            modelSC.code = "";
        }
        else
        {
            try
            {
               modelSC.code = System.IO.File.ReadAllText(Server.MapPath(modelSC.path));
            }
            catch (Exception e)
            {
                modelSC.code = "";
            }
        }
        return PartialView(modelSC);

    }

我做错了什么?

【问题讨论】:

  • 你能不能把这个脚本放到主视图页面而不是局部视图中
  • 这个脚本在主视图中,它调用局部视图。
  • 猜你检查控制台窗口是为了给你一个关于发生了什么的提示!
  • 其实这是我的第一个网站,一直在尝试,还不知道怎么排查问题。

标签: javascript razor asp.net-mvc-5


【解决方案1】:

我通过将主视图中的脚本重新排序解决了这个问题,如下所示:

    <script type="text/javascript" src="~/Scripts/jquery-3.1.1.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
<script type="text/javascript" src="~/Scripts/jquery.unobtrusive-ajax.js"></script>

jquery.unobtrusive 是第一个。然后我更新了加载局部视图的脚本:

<script type="text/javascript">
     $(document).ready(function () {
         $('.filelist').on('click', function (e) {
             e.preventDefault();
             alert('Im clicked on filePath = ' + $(this).attr('value'));
             var filePath = $(this).attr('value');    //value is attribute set in Html
             $('#partial').load('@Url.Action("DevelopmentPartial", "Development")', { path1: filePath });
         });
     });
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 2017-06-14
    相关资源
    最近更新 更多