我将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( “关于”) 但它不适用于创建评论页面,我无法通过Model.Comments
答案 0 :(得分:1)
如果您想在另一个表单中嵌入/导入一个表单,那么
1)您可以使用部分视图
2)对于部分视图,您不使用主/网站布局/主页,否则它看起来在主视图中复制的网站很难看!因此,您可以在局部视图中将布局设置为null:
@{
Layout = null;
}
3)现在要渲染局部视图,您可以为html助手@ Html.RenderPartial(...)指定局部视图的名称。如果你需要传入,这个助手的一个重载也接受Model对象。例如如果你想将评论列表传递给部分视图&#34; _Comments.cshtml&#34;然后:
@{ Html.RenderPartial("_Comments.cshtml", Model.CommentsList)}
//Assuming Model.CommentsList is of type: IEnumerable<Comment>
将部分视图名称加上下划线是一般惯例:&#34; _&#34;
4)同时确保传递到上面的局部视图的模型对象和部分视图接受的模型类型是兼容的。
e.g。
@model IEnumerable<Comment>
注意:我的答案中的所有对象/类型和名称都是为了向您提供一些细节而选择的,可能不一定适合您。
希望这会有所帮助......
答案 1 :(得分:0)
在视图上使用此代码:
@Html.Action("View page name","Controller name")
将其粘贴到您要“导入”
的视图上@{
ViewBag.Title = "NameOfView";
Layout = null;
}
答案 2 :(得分:-1)
@using (Html.BeginForm("Upload", "Upload", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
Select a file <input type="file" name="file" />
<input type="submit" value="Upload" />
</fieldset>
}