2012-10-30 17 views
1

我試圖從我的視圖中發出ajax請求,這會在&後同時獲取一些數據。我想使用檢索到的數據更新該視圖頁面上的div內容。下面是代碼:在mvc中使用不引人注目的ajax時出錯3

筆者認爲:

<div id="NewComment" style="height: auto; width: 850px; background-color: Silver; 
    margin-top: 10px;"> 
    @{ 
     AjaxOptions ajaxopts = new AjaxOptions { UpdateTargetId = "Comments" }; 
    } 
    @using (Ajax.BeginForm("NewComment", ajaxopts)) 
    { 
     @Html.ValidationSummary() 
     <span style="margin-left: 10px"> 
      @Html.TextAreaFor(x => x.Comments.Comments, new { @class = "Comment" }) 
     </span> 
     @Html.TextBoxFor(x => x.Posts.PostID, new { @class = "PID" }) 

     <br /> 
     <span style="margin-left: 10px"> 
      <input type="submit" value="Post" style="height: 50px; width: 100px; text-align: center; 
       font-size: larger" /></span> 

    } 
</div> 
<div id="Comments"> 
    @{ 
     if (Model.CommentList != null) 
     { 
      foreach (var y in Model.CommentList) 
      { 
     <div id="DisplayComment" style="height: auto; width: 500px; background-color: #559988; 
      margin-top: 10px; margin-left: 10px;"> 
      @y.UserName says: 
      <br /> 
      @y.Comment1 
      <br /> 
     </div> 
      } 
     } 
     else 
     { 
     <div id="DisplayNoComment" style="height: auto; width: 500px; background-color: Yellow; 
      margin-top: 10px; margin-left: 10px;"> 
      @Html.Label("Be First to make a comment") 
     </div> 
     } 
    } 
</div> 

我的控制器:

[HttpPost] 
    public ActionResult NewComment(PostComment model) 
    { 
     var business = new Business(); 
     var entity = new Comment(); 

     //**For inserting comments 
     entity.Comment1 = model.Comments.Comments; 
     entity.PostID = model.Posts.PostID; 
     entity.UserID = business.GetUserID(User.Identity.Name); 
     entity.UserName = User.Identity.Name; 

     business.PostComment(entity); 
     //For inserting comments** 


     //**for getting comments 
     var viewmodel = new PostComment(); 
     if (business.GetAllComment(model.Posts.PostID) == null) 
     { 
     } 
     else 
     { 
      viewmodel.CommentList = business.GetAllComment(model.Posts.PostID); 
     } 
     //for getting comments** 

     return View("Comments", viewmodel); 
    } 

我回國命名爲所需的數據評論的局部視圖,一切工作正常,但有一個有線問題。雖然我在部分視圖中沒有使用任何母版頁,但仍顯示母版頁的內容。因此,原始視圖頁面顯示來自母版頁的內容,如頁眉&頁腳兩次。請幫忙。

回答

0

在您看來,包括頂部

@{ 
    Layout = null; 
} 
+0

它的工作下面的一行代碼!但由於我沒有提及使用任何母版頁,爲什麼它仍然使用一個? – user1784622

相關問題