我只是在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>
这是showhide partial:
<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 :(得分: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,然后期望按钮通过getElementById("partial")
相反,您需要为每个<div>
生成唯一ID,然后在javascript方法中使用相同的ID getElementById()