我有以下观点:
@model IEnumerable<YIS2.Models.Testimonial>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div id="Testimonials">
<h2>Our Testimonials</h2>
@foreach (var item in Model)
{
<blockquote>
<p>@item.Content</p>
<p>@item.AuthorName</p>
</blockquote>
}
</div>
<div id="SubmitTestimonial">
<h2>Submit Testimonial</h2>
@using (Html.BeginForm("NewTestimonial", "Testimonial", FormMethod.Post))
{
@Html.EditorFor(m => Model.AuthorName)
@Html.EditorFor(m => Model.AuthorEmail)
@Html.EditorFor(m => Model.Content)
<input type="submit" id="submitTestimonial" />
}
Run Code Online (Sandbox Code Playgroud)
我需要模型为IEnumerable,因此我可以遍历内容以显示以前保存的推荐.问题是我在语句m => Model.x上出错,因为Model是IEnumerable.
什么是最好的修复方法?
如果您需要回发一个单一的 Testimonial使用IEnumerable<Testimonial>是行不通的.我建议你创建一个组合视图模型并传递它,即
public class AddTestimonialViewModel
{
public IEnumerable<Testimonial> PreviousTestimonials { get; set; }
public Testimonial NewTestimonial { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后在你看来
@model YIS2.Models.AddTestimonialViewModel
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div id="Testimonials">
<h2>Our Testimonials</h2>
@foreach (var item in Model.PreviousTestimonials)
{
<blockquote>
<p>@item.Content</p>
<p>@item.AuthorName</p>
</blockquote>
}
</div>
<div id="SubmitTestimonial">
<h2>Submit Testimonial</h2>
@using (Html.BeginForm("NewTestimonial", "Testimonial", FormMethod.Post))
{
@Html.EditorFor(m => m.NewTestimonial.AuthorName)
@Html.EditorFor(m => m.NewTestimonial.AuthorEmail)
@Html.EditorFor(m => m.NewTestimonial.Content)
<input type="submit" id="submitTestimonial" />
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
842 次 |
| 最近记录: |