Gần đây tôi đã bắt đầu chơi xung quanh với ASP.net MVC (4), nhưng tôi không thể quấn đầu về vấn đề này mà tôi gặp phải. Tôi chắc rằng nó dễ dàng khi bạn biết điều đó.Sử dụng các khung nhìn một phần trong ASP.net MVC 4
tôi về cơ bản đang cố gắng làm những điều sau đây theo quan điểm của Index của tôi:
- Danh sách các mặt hàng hiện tại trong cơ sở dữ liệu của kiểu "Ghi chú" trong giao diện Index (đó là dễ dàng)
- Tạo mới các mục trong cùng một khung nhìn Index (không dễ dàng như vậy).
Vì vậy, tôi figured tôi cần một cái nhìn cục bộ, và rằng tôi đã tạo ra như sau (_CreateNote.cshtml):
@model QuickNotes.Models.Note
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Note</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Content)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Content)
@Html.ValidationMessageFor(model => model.Content)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
Theo quan điểm của Index ban đầu của tôi (Index.cshtml) Tôi đang cố gắng để hiển thị chế độ xem một phần này:
@model IEnumerable<QuickNotes.Models.Note>
@{
ViewBag.Title = "Personal notes";
}
<h2>Personal notes</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Content)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Content)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
<div>
@Html.Partial("_CreateNote")
</div>
(sử dụng: @ Html.Partial ("_ CreateNote")) Tuy nhiên. Đây dường như không làm việc, khi tôi nhận được thông báo lỗi sau:
Line 35:
Line 36: <div>
Line 37: @Html.Partial("_CreateNote");
Line 38: </div>
Source File: c:\Dropbox\Projects\workspace .NET MVC\QuickNotes\QuickNotes\Views\Notes\Index.cshtml Line: 37
Stack Trace:
[InvalidOperationException: The model item passed into the dictionary is of type 'System.Data.Entity.DbSet`1[QuickNotes.Models.Note]', but this dictionary requires a model item of type 'QuickNotes.Models.Note'.]
System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) +405487
NotesController của tôi trông như thế này:
public ActionResult Index()
{
var model = _db.Notes;
return View(model);
}
//
// GET: /Notes/Create
public ActionResult Create()
{
return View();
}
//
// GET: /Notes/_CreateNote - Partial view
public ViewResult _CreateNote()
{
return View("_CreateNote");
}
tôi nghĩ rằng nó đã làm với thực tế là quan điểm thị trường đang tiếp bằng cách sử dụng mô hình khác nhau, như trong @model IEnumerable, nhưng không có vấn đề làm thế nào tôi thay đổi nó xung quanh, bằng cách sử dụng RenderPartial, RenderAction, thay đổi ActionResult để ViewResult vv, tôi không thể làm cho nó hoạt động.
Bất kỳ mẹo nào sẽ được đánh giá rất nhiều! Vui lòng cho tôi biết nếu bạn cần thêm thông tin. Tôi rất sẵn lòng nén toàn bộ dự án nếu cần.
Bạn là chính xác! Cảm ơn rất nhiều! Tôi thực sự đã cố gắng gần như một loạt các lần, nhưng không hình tôi cần câu "mới". Ngu ngốc tôi: ( –