ASP.NET MVC -Refresh CodeMirror编辑器onclick

时间:2017-03-15 17:16:01

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

我在局部视图中有一个codemirror编辑器,在主视图中有一个文件列表。我想在单击文件名后刷新编辑器。我在StackOverflow和其他网站上尝试了很多解决方案,但没有任何效果,这是我第一次使用Javascript,所以我无法弄清楚我做错了什么。

这是我的代码:

控制器:

public ActionResult Index()
        {
            StudentsCodes model = new StudentsCodes();
            model.Student = (Student)CurrentUser;
            var user = UserManager.FindById(((Student)CurrentUser).InstructorID);
            model.Instructor =(Instructor) user;
            return View(model);
        }
public PartialViewResult DevelopmentPartial (StudentsCodes path )
        {
            return PartialView(path);
        }

主要观点:

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

    <ul id="tree">
        @foreach (var file in Directory.GetFiles(Server.MapPath("~/Content/" + Model.Student.UserName + "/CompilerProject/" + name)))
            {
                var filename = Path.GetFileName(file);
                <li id="filelist" onclick="@(Model.path = "~/Content/" + Model.Student.UserName + "/CompilerProject/src/" + @filename)">
                    <span class="glyphicon glyphicon-file"></span>
                    @filename
                /li>
                }

    <div id="partial">
        @{
            Html.RenderPartial("DevelopmentPartial",null);
        }

    </div>
    <script>
        $(document).ready(function () {
            $("#filelist").click(function (e) {
               @{Html.RenderAction("DevelopmentPartial", Model);
               }
            });
        });
    </script>

部分观点

@using (Html.BeginForm())
        {
            var fileContents= "";
            if (Model==null)
            {
                fileContents = "";
            }
            else
            {
                fileContents = System.IO.File.ReadAllText(Server.MapPath(Model.path));
            }
                @Html.TextArea("code", fileContents, new { id = "code" })
        }

我无法为列表元素分配 id ,因为它们的编号在编译时是未知的,并且在用户添加或删除文件时会发生变化,这就是为什么大多数解决方案都没有工作。结果是3个编辑器重叠并显示最后一个文件的内容。并且<li>项是不可点击的。我的代码中我做错了什么?

修改: 按以下步骤更新脚本后:

<script>
        $(document).ready(function() {
            $(".filelist").on("click",function (e) {
                $("#partial").load('DevelopmentPartial');

            });
        });
    </script>

它刷新部分视图,但编辑器始终为空,模型始终为 null 。使用“onclick”更新模型是错误的吗?

1 个答案:

答案 0 :(得分:0)

如果有人遇到同样的问题,我通过在列表中将id更改为class来解决它,然后使用此脚本:

 <div id="partial">
    @{
        Html.RenderAction("DevelopmentPartial", new { path1 = Model.path});
    }
</div>


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

控制器:

 public PartialViewResult DevelopmentPartial(string path1)
    {
        modelSC.path = path1;
        return PartialView(modelSC);

    }

其中modelSC是控制器中的全局变量。