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