2013-01-10 34 views
0

我只在MVC4和 开始我试图把多个部分视图放在一个页面中每个部分都假设是与不同的数据,但它总是显示一个部分只有一个数据 每个部分显示在按钮点击。如何将多个部分放在一个页面中? MVC4

这是视图代码:

  <section id="comments"> 
      <h3>Comments</h3> 
      @{ 

       foreach (MvcApplication4.Models.comment item in postComments) 
       { 
          <article> 

           <p>@item.body</p> 

       using (Html.BeginForm("Delete", null, new { id = item.ID ,pid = item.idPost}, FormMethod.Post)) 
       { 
        @Html.HttpMethodOverride(HttpVerbs.Delete) 
         <button type="submit" class="button delete" >Delete</button> 
              } 

        <section> 
         <button type="submit" class="button Edit" id="edit" onclick="return showHide();" >Edit</button> 

         <div id="partial" hidden="hidden"> 
           @Html.Partial("editCom",item) 
          </div> 
        </section> 
       } 
      } 
      </article> 

     } 
     } 
     </section> 

这是显示隐藏部分:

<script type="text/javascript" language="javascript">// <![CDATA[ 
     function showHide() { 
      var ele = document.getElementById("partial"); 
      if(ele.style.display == "block") { 
       ele.style.display = "none"; 
      } 
      else { 
       ele.style.display = "block"; 
      } 
     } 
     // ]]></script> 

这是部分:

@using MvcApplication4.Models 
@model MvcApplication4.Models.comment  


    @{comment com = Model; 
     } 
      @using (Html.BeginForm("Edit", null,new{ pid=com.idPost,cid=com.ID}, FormMethod.Post)) 
               { 
       <fieldset> 
          <div class="editor-label"> 
      @Html.LabelFor(x => com.user) 
     </div> 
     <div class="editor-field"> 
      @Html.LabelFor(x => com.user,Session["username"].ToString()) 
      @Html.ValidationMessageFor(x => com.user) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(x => com.email) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(x => com.email,com.email) 
      @Html.ValidationMessageFor(x => com.email) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(x => com.url) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(x => com.url,com.url) 
      @Html.ValidationMessageFor(x => com.url) 
     </div> 
     <div class="editor-label"> 
     @Html.LabelFor(x => com.body) 
     </div> 

     <div class="editor-field"> 
      @Html.EditorFor(x => com.body,com.body) 
      @Html.ValidationMessageFor(x => com.body) 
     </div> 
        <p> 
      <input type="submit" value="save" /> 

     </p> 
       </fieldset> 
    } 

回答

0

您的代码为

<div id="partial" hidden="hidden"> 
    @Html.Partial("editCom",item) 
</div> 

确定哪些部分由按钮controled:

var ele = document.getElementById("partial"); 
if(ele.style.display == "block") { 
    ele.style.display = "none"; 
} 
.... 

要生成若干个部分全部用的“部分”相同的ID,然后在期待按钮,确定没有某种形式的唯一ID来管理其格通过getElementById("partial")

相反,你需要生成每个<div>一个唯一的ID,然后用相同的ID在你的JavaScript方法getElementById()

相关问题