2014-03-07 45 views
0

在我看來,我想用瀏覽按鈕手動打開一個xml文件,然後能夠通過提交按鈕將xml的內容發送到文本區域。ASP.NET MVC打開一個xml並顯示在textarea

這是我的一些代碼,但我不知道如何在我的文本區域顯示xml內容。

查看:

@using (Html.BeginForm("OpenFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    <input type="file" name="file" id="file"/> 
    <input type="submit" value="OK"/> } 

@Html.TextAreaFor(x => x.XMLContent, 15, 80, null)<p> 

控制器:

[HttpPost] 
public ActionResult OpenFile(HttpPostedFileBase file) 
{ 
    string content = string.Empty; 

    // Verify that the user selected a file 
    if (file != null && file.ContentLength > 0) 
    { 
     // extract file path 
     var filePath = Path.GetFullPath(file.FileName); 

     using (StreamReader reader = new StreamReader(filePath)) 
     { 
      content = reader.ReadToEnd(); 
     } 
    } 

    return View("Index"); 
} 

回答

1

您需要將XML傳遞給視圖莫名其妙。 一個簡單的選擇是使用動態ViewBag。 於是久違的觀點之前,你可以補充一點:

ViewBag.content = content; 

,然後在視圖:

@Html.TextArea("TextAreaName", (string)ViewBag.content) 
+0

它的工作原理,非常感謝! – user3391714

+0

請標記答案爲接受,如果它可以幫助你。謝謝! – frikinside