我在C#中使用2个aspx页面之间进行了此操作,但现在我将其更改为弹出窗口,它不再起作用了。它是一个xml文件,我试图在codemirror编辑器中显示。 看起来javascript在pop弹出之前正在运行,并且它正在采用空的textarea并渲染codemirror。我不确定,所以希望有人为我提供解决方案。
我的观点是这样的:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js" type="text/javascript"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css"/>
<link rel="stylesheet" href="<%= Html.IncludeFile("Scripts", "Codemirror/codemirror.css") %>" />
<script type="text/javascript" src="<%= Html.IncludeFile("Scripts", "Codemirror/codemirror.js")%>"></script>
<script type="text/javascript" src="<%= Html.IncludeFile("Scripts", "Codemirror/xml.js")%>"></script>
<script type="text/javascript">
function init() {
$(document).ready(
function () {
$("#result").dialog({
autoOpen: false,
title: 'Edit the table',
width: 500,
open: GetXml(),
height: 'auto',
modal: true
});
});
openPopup();
}
function openPopup() {
$("#result").dialog("open");
}
function GetXml() {
//var selec = $("Selected").val($("FileName").val());
var Selected = document.getElementById("FileName").value;
$.ajax({
type: "POST",
url: "GetXmlFile",
data: { Selected: $('#FileName').val() }
})
.done(function (msg) {
// alert("Data Saved: " + msg);
$('#File').html(msg);
});
}
function SaveChanges() {
alert('Get the values and post them back to the server to be saved!');
$("#result").dialog("close");
}
</script>
<style type="text/css">
.CodeMirror {
border:1px solid #334;
width:auto;
}
</style>
</head>
<body>
<div id="result" style="display:none">
<% using (Html.BeginForm("Index","EditFile")) %>
<% { %>
<%= Html.TextAreaFor(m => m.File) %>
<%= Html.ValidationMessageFor(m => m.File)%>
<input type="submit" value="Save" />
<% } %>
<script type="text/javascript">
var editor = CodeMirror.fromTextArea(document.getElementById("File"), {
lineNumbers: true,
matchBrackets: true,
mode: "text/xml"
});
</script>
</div>
<table style="width:100%;">
<tr>
<td style="width:15px;"> </td>
<td>
<% using (Html.BeginForm()) %>
<% { %>
<ul>
<li>
<%= Html.LabelFor(m => m.FileName) %>
<%=Html.DropDownListFor(m => m.FileName,new SelectList(System.IO.Directory
.EnumerateFiles(@"C:\Something", "*")
.Select(System.IO.Path.GetFileNameWithoutExtension)), "Please select one")%>
<%=Html.ValidationMessageFor(m => m.FileName)%>
</li>
</ul>
<% } %>
<input type="button" onclick="init()" value="open box">
</td>
</tr>
</table>
</div>
控制器中调用的方法:
[HttpPost]
public String GetXmlFile(String Selected)
{
String selectedFileName = @"C:\Something\" + Selected + ".xml";
String line = "";
String[] linesInFile = System.IO.File.ReadAllLines(selectedFileName);
foreach (var lines in linesInFile)
{
line = String.Concat(line, lines + Environment.NewLine);
}
return line;
}
有什么想法吗?
如果无法做到这一点,那么您会建议我使用哪个编辑器在文件中显示xml和编辑。