0

我只是从 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>
   }
4

1 回答 1

0

你的代码

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

确定哪些部分由按钮控制:

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

您正在生成几个部分,所有部分都具有相同的“部分”ID,然后期望按钮确定要管理的 div 没有某种唯一 ID 通过getElementById("partial")

相反,您需要为每个生成一个唯一的 id <div>,然后在您的 javascript 方法中使用相同的 idgetElementById()

于 2013-01-10T21:17:33.850 回答