ASP.net MVC 4에서 부분보기 사용
저는 최근에 ASP.net MVC (4)를 가지고 놀기 시작했지만 제가 겪고있는이 문제에 대해 머리를 감쌀 수는 없습니다. 나는 당신이 그것을 알고있을 때 그것이 쉽다고 확신합니다.
본질적으로 인덱스보기에서 다음을 수행하려고합니다.
- 인덱스보기에서 "Note"유형의 데이터베이스에있는 현재 항목을 나열합니다 (간단 함).
- 동일한 인덱스보기에서 새 항목 만들기 (쉽지 않음).
그래서 부분보기가 필요하다고 생각했고 다음과 같이 만들었습니다 (_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>
}
내 원래 인덱스보기 (Index.cshtml)에서이 부분보기를 렌더링하려고합니다.
@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>
(사용 : @ Html.Partial ( "_ CreateNote")) 그러나. 다음 오류 메시지가 표시되므로 작동하지 않는 것 같습니다.
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는 다음과 같습니다.
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");
}
I think it has to do with the fact that the Index view is using the model differently, as in @model IEnumerable, but no matter how I change it around, using RenderPartial, RenderAction, changing ActionResult to ViewResult etc, I can't get it to work.
Any tips would be greatly appreciated! Please let me know if you need any more information. I'd be happy to zip down the entire project if needed.
Change the code where you load the partial view to:
@Html.Partial("_CreateNote", new QuickNotes.Models.Note())
This is because the partial view is expecting a Note but is getting passed the model of the parent view which is the IEnumerable
You're passing the same model to the partial view as is being passed to the main view, and they are different types. The model is a DbSet
of Note
s, where you need to pass in a single Note
.
You can do this by adding a parameter, which I'm guessing as it's the create form would be a new Note
@Html.Partial("_CreateNote", new QuickNotes.Models.Note())
참고URL : https://stackoverflow.com/questions/13934671/using-partial-views-in-asp-net-mvc-4
'programing tip' 카테고리의 다른 글
최대는 얼마입니까? (0) | 2020.10.14 |
---|---|
java.lang.IllegalArgumentException : 경로 구분 기호 포함 (0) | 2020.10.14 |
Volley JsonObjectRequest Post 요청이 작동하지 않습니다. (0) | 2020.10.14 |
Fowler의 "엔터프라이즈 애플리케이션 아키텍처 패턴"은 여전히 관련이 있습니까? (0) | 2020.10.14 |
APN 디바이스 토큰은 개별 앱마다 고유합니까? (0) | 2020.10.14 |