【发布时间】:2015-10-04 18:12:57
【问题描述】:
我有 BlogHomePage.cshtml 作为主页
@model IEnumerable<WebApplication1.Models.Post>
@{
ViewBag.Title = "BlogHomePage";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div id="content">
<!--Content of the blog-->
@foreach (var post in Model)
{
<div id="mainContent">
<section>
<article class="blogPost">
<header>
<h2>@Html.DisplayFor(modelItem => post.PostTitle)</h2>
<p>Posted on @Html.DisplayFor(modelItem => post.PostDate) by <a target="_new" href=@Html.DisplayFor(modelItem => post.WebSite)>@Html.DisplayFor(modelItem => post.PostAuthor)</a> - <a href="#@Html.DisplayFor(modelitem => post.PostID)">@Html.DisplayFor(modelItem => post.Comments.Count) comments</a></p>
</header>
<div>
<p class="blogContent">@Html.DisplayFor(modelItem => post.PostText)</p>
</div>
@if (!post.PostImage.IsEmpty())
{
<img src="@Url.Content(post.PostImage)" alt="@post.PostAuthor" width="500" />
}
<br/>
@if (!post.PostVideo.IsEmpty())
{
<img src="@Url.Content(post.PostVideo)" alt="@post.PostVideo" width="500" />
}
</article>
<!----creating an anchor on the page to point to comments of the post -->
<a name="@Html.DisplayFor(modelitem => post.PostID)"></a>
</section>
</div>
//Change background of odds comments
bool counter = true;
foreach (var comment in post.Comments)
{
if (counter)
{
counter = false;
}
else
{
counter = true;
}
<section id="@(counter==true ? "commentstrue":"comments")">
<!--Comments of bloggers-->
<article class="commentswidth">
<header>
<h3>@Html.DisplayFor(modelComment => comment.CommentTitle)</h3>
<a target ="_new" href=@Html.DisplayFor(modelComment => comment.CommentWebSite)>@Html.DisplayFor(modelComment => comment.CommentAuthor)</a>
</header>
<p>@Html.DisplayFor(modelComment => comment.CommentText)</p>
</article>
</section>
}
<br />
<!--Comments form-->
}
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我想从我的 CommentsController 上的“创建”操作中“导入”我的 htmlbeginform
@model WebApplication1.Models.Comment
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Comment</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.PostID, "PostID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("PostID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.PostID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CommentTitle, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CommentTitle, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CommentTitle, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CommentAuthor, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CommentAuthor, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CommentAuthor, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CommentWebSite, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CommentWebSite, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CommentWebSite, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CommentText, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CommentText, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CommentText, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
这条线工作,但我可以看到页面内页(包括布局),我不想要它。 @Html.Action("创建","评论")
这条线在没有关于页面布局的情况下工作: @Html.Partial("关于") 但它不适用于创建 cmets 页面,我无法传递 Model.Comments
【问题讨论】:
-
你能详细说明一下,为什么要“导入” htmlbeginform ?
-
我有博文主页。在此页面中,我看到很少的博客、cmets 和“发表评论表”。对于模型中的每个项目,我都可以看到博客。我无法访问此视图的 cmets,因为我已经有一个模型(博客)。我想在“评论文件夹”上导入视图“创建评论”的 html 开始表单。我可以导入任何没有模型的视图,但我无法导入带有模型的视图。要恢复,我想在此页面的其他页面中查看表单。 @SivaGopal
标签: asp.net-mvc render partial