2016-07-06 48 views
1

我綁定MVC中的DATA通過下面的代碼顯示:數據未在文字區域

@model DocumentManagementSystem.Models.DocumentFileName 
@{ 
    ViewBag.Title = "Index"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

@using (Html.BeginForm("Upload", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    <table style="margin-top:50px;"> 
     <tr> 
      <td> 
       <input type="file" name="file" /> 
      </td> 
      <td> 
       <input type="submit" name="Submit" id="Submit" value="Upload" /> 
      </td> 
     </tr> 
    </table> 
    @Html.TextAreaFor(f => f.Data, new { ViewBag.DocumentData }) 
} 

綁定數據是從Viewbag。我給這個值textarea。它顯示源代碼中使用F12,但它不顯示在UI部分。我們已經附上截圖爲:

shows in code but not in ui

我已經強調這是不是同一textarea看到的價值。

下面是我的控制器代碼,通過它我的數據發佈到Viewbag:

 [HttpPost] 
     public ActionResult Upload() 
     { 
      if (Request.Files.Count > 0) 
      { 
       var file = Request.Files[0]; 
       object filename = Path.Combine(Server.MapPath("~/Document/"), file.FileName); 
       Microsoft.Office.Interop.Word.ApplicationClass AC = new Microsoft.Office.Interop.Word.ApplicationClass(); 
       Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document(); 
       object readOnly = false; 
       object isVisible = true; 
       object missing = System.Reflection.Missing.Value; 
       var retString = ""; 
       try 
       { 
        doc = AC.Documents.Open(ref filename, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible); 
        retString = doc.Content.Text; 
       } 
       catch (Exception ex) 
       { 
        retString = ex.ToString(); 
       } 
       ViewBag.DocumentData = new MvcHtmlString(System.Web.HttpUtility.HtmlDecode(retString.ToString())); 
      } 
      else 
      { 
       ViewBag.result = "Document Not Support"; 
      } 
      return View("Index"); 

     } 

回答

0

你不能真正做到這一點與textareafor據我所知,因爲它必然的價值Model.Data

在使用textareafor之前,您是否可以不聲明您的Model.Data = ViewBag.DocumentData

編輯:E.g:

@model DocumentManagementSystem.Models.DocumentFileName 
@{ 
    ViewBag.Title = "Index"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
    Model.Data = ViewBag.DocumentData; 
} 

@using (Html.BeginForm("Upload", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 

    @Html.TextAreaFor(f => f.Data) 
} 
+0

我沒有得到你,請解釋一下。 – Bhupendra

+0

您已在剃刀文件頂部定義了一個模型。 'DocumentManagementSystem.Models.DocumentFileName'。 Your TextAreaFor綁定到該模型的「Data」屬性。 如果您在使用TextAreaFor之前設置了'Model.Data = ViewBag.DocumentData',那麼生成的文本區域將具有設置爲'ViewBag.DocumentData'的任何值。 –

+0

我在哪裏設置'Model.Data = ViewBag.DocumentData' – Bhupendra