简体   繁体   中英

ASP.NET MVC -Refresh CodeMirror editor onclick

I have a codemirror editor in a partial view and a list of files in the main view. I want to refresh the editor once a file name is clicked. I tried many solutions provided on StackOverflow and other websites but nothing worked , and This is my first time using Javascript so I can't figure out What am I doing wrong.

This is my code:

Controller:

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);
        }

Main view:

<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>

partial view:

@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" })
        }

I can't assign id s for list elements since their number is unknown at compile time and it changes when the user adds or deletes a file, that's why most of the solutions provided didn't work . The result here was 3 editors overlapping and display the contents of the last file. And <li> items are non-clickable. What am I doing wrong in my code ?

Edit : After updating the script as the following:

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

            });
        });
    </script>

It refreshes the partial view but the editor is always empty, and the Model is always null . Is it wrong to update the Model using "onclick"?

In case someone faced the same problem, I solved it by changing id to class at the list, then by using this script:

 <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>

And the controller:

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

    }

where modelSC is a global variable in the controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM