none
Razor - Updating partial view when clicking <li> element.

    Question

  • I have the following view:

    I want to view the contents of a file once it's clicked. This is my code so far :

    Index view:

    @foreach (var file in Directory.GetFiles(Server.MapPath("~/Content/" + Model.Student.UserName + "/CompilerProject/" + name))) { var filename = Path.GetFileName(file); <li class="filelist" value="@(Model.path = "~/Content/" + Model.Student.UserName + "/CompilerProject/src/" + @filename)"> <span class="glyphicon glyphicon-file"></span> @filename </li> }<div id="partial"> @{ Html.RenderPartial("DevelopmentPartial", Model); } </div> <!-- --> <script> $(document).ready(function () { $(".filelist").on("click",function (e) {

      var filepath = (this).value;

    @Model.path=filepath; $("#partial").load('DevelopmentPartial',{model = @Model}); }); }); </script>


    Partial view:

    </div>
            @using (Html.BeginForm())
            {
                var fileContents = "";
                if (Model == null || Model.path==null)
                {
                    fileContents = "";
                }
                else
                {
                    //var filename = Path.GetFileName("");
                    try
                    {
                        fileContents = System.IO.File.ReadAllText(Server.MapPath(Model.path));
                    }
                    catch(Exception e)
                    {
                        fileContents = "";
                    }
                }
                    @Html.TextArea("code", fileContents, new { id = "code" })
            }
    
    </div>
    When I click on a file, the editor refreshes but empty. I debugged the code and found that the value passed to the controller is always null . How to make it work?


    Monday, March 20, 2017 5:40 PM

All replies

  • Hi AhH0,

    This forum discusses and asks questions about .NET Framework Base Classes, Since your issue is more related to asp.net MVC, I would suggest that you could post your issue on asp.net MVC forum for suitable support.

    https://forums.asp.net/1146.aspx/1?MVC

    Thanks for your understanding and cooperation.

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 21, 2017 1:16 AM