如何使用@model IEnumerable的&LT;&GT;和@model&LT;&GT;在同一页面(列表/创建)MVC3 [英] How to use @model IEnumerable<> and @model <> in the same page (List / Create) MVC3
问题描述
可以说我有一类位置,而我使用MVC3创建一个脚手架列表(index.cshtml)。该指数的HTML页面使用@model IEnumerable的。如果我想要一个新的位置添加到列表中,我preSS创建和使用我@model Project.Models.Location结束与一个新的html页面。
而不是去到一个新的页面,我想有创建页面,使用自举(Twitter)上的模态弹出。我已经使用partialview没有任何运气尝试。不管我做什么我得到的字典的错误消息。
传递到字典的模型项的类型是System.Data.Entity.Infrastructure.DbQuery`1 [Project.Models.Location]',但本词典需要类型'Project.Models.Location的典范项目
当我不取的partialview本身是工作模态(引导)。作为生成的创建(CRUD)的partialview本身是完全相同的。
编辑:我重写了整个事情。
指数:
@model IEnumerable的&LT; LoLStats.Models.Location&GT;@ {
ViewBag.Title =指数;
}
@foreach(以型号VAR项){
&LT; UL&GT;
&LT;立GT; @ Html.DisplayFor(modelItem =&GT; item.LocationName)LT; /李&GT;
&LT; / UL&GT;
}&LT;一类=BTN数据切换=模态的href =#myModal&GT;创建&LT; / A&GT;&LT; DIV CLASS =模式隐藏ID =myModal&GT;
&LT; DIV CLASS =模头&GT;
&LT;按钮式=按钮级=关闭数据解雇=模式&GT;&×LT; /按钮&GT;
&LT; H3&GT;模态头&LT; / H3 GT&;
&LT; / DIV&GT;
&LT; DIV CLASS =模体&GT;
&所述p为H.; @ Html.Partial(_ createLocation)&所述; / P&GT;
&LT; / DIV&GT;
&LT; DIV CLASS =模式躯&GT;
&LT; A HREF =#类=BTN数据解雇=模式&GT;关闭&LT; / A&GT;
&LT; A HREF =#类=BTN BTN-小学&GT;保存更改LT; / A&GT;
&LT; / DIV&GT;
&LT; / DIV&GT;
_createLocation:
@model LoLStats.Models.Location
@ {
ViewBag.Title =创建; }
@using(Html.BeginForm()){
@ Html.ValidationSummary(真)
位置&LT; DIV CLASS =编辑标记&GT;
@ Html.LabelFor(型号=&GT; model.LocationName)
&LT; / DIV&GT;
&LT; DIV CLASS =主编场&GT;
@ Html.EditorFor(型号=&GT; model.LocationName)
@ Html.ValidationMessageFor(型号=&GT; model.LocationName)
&LT; / DIV&GT;
&所述p为H.;
&LT;输入类型=提交值=创建/&GT;
&所述; / P&GT;
&LT; /字段集&GT; }
块引用>解决方案您可以让一个具有两个属性视图模型:
- 了IEnumerable父视图
- 的模式一个独具特色的对象
然后强类型既欣赏到视图模型,你应该准备就绪。
修改
视图模型:
公共类MyViewModel()
{
公共IEnumerable的&LT; LoLStats.Models.Location&GT;位置{搞定;组;}
公共LoLStats.Models.Location位置{搞定;组;}
}指数:
@model myNamespace.MyViewModel
...
@foreach(在Model.Locations VAR项){
&LT; UL&GT;
&LT;立GT; @ Html.DisplayFor(modelItem =&GT; item.Locations.LocationName)LT; /李&GT;
&LT; / UL&GT;
..._createLocation:
@model myNamespace.MyViewModel
...
&LT; DIV CLASS =编辑标记&GT;
@ Html.LabelFor(型号=&GT; model.Location.LocationName)
&LT; / DIV&GT;
&LT; DIV CLASS =主编场&GT;
@ Html.EditorFor(型号=&GT; model.Location.LocationName)
@ Html.ValidationMessageFor(型号=&GT; model.Location.LocationName)
&LT; / DIV&GT;
...我希望这是足够的细节,让你去
Lets say i have a class 'Location', and I'm using MVC3 to create a scaffolding list(index.cshtml). The index html page uses @model IEnumerable. If i want to add a new location to the list, i press create, and i end up with a new html page using @model Project.Models.Location.
Instead of going to a new page, i would like to have the create page as a popup using modal in boostrap(twitter). I've tried using partialview without any luck. Whatever i do i get the dictionary error message.
The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[Project.Models.Location]', but this dictionary requires a model item of type 'Project.Models.Location'.
The modal(bootstrap) itself is working when i'm not fetching the partialview. The partialview itself is exactly the same as the generated create(CRUD).
EDIT: I rewrote the whole thing.
Index:
@model IEnumerable<LoLStats.Models.Location> @{ ViewBag.Title = "Index"; } @foreach (var item in Model) { <ul> <li>@Html.DisplayFor(modelItem => item.LocationName)</li> </ul> } <a class="btn" data-toggle="modal" href="#myModal" >Create</a> <div class="modal hide" id="myModal"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> <h3>Modal header</h3> </div> <div class="modal-body"> <p>@Html.Partial("_createLocation")</p> </div> <div class="modal-footer"> <a href="#" class="btn" data-dismiss="modal">Close</a> <a href="#" class="btn btn-primary">Save changes</a> </div> </div>
_createLocation:
@model LoLStats.Models.Location
@{ ViewBag.Title = "Create"; }
@using (Html.BeginForm()) { @Html.ValidationSummary(true) Location
<div class="editor-label"> @Html.LabelFor(model => model.LocationName) </div> <div class="editor-field"> @Html.EditorFor(model => model.LocationName) @Html.ValidationMessageFor(model => model.LocationName) </div> <p> <input type="submit" value="Create" /> </p> </fieldset> }
解决方案You could make a view model that has two properties:
- The IEnumerable for the parent view
- A singular object for the modal
Then strongly type both views to the view model and you should be all set.
Edit
ViewModel:
public class MyViewModel() { public IEnumerable<LoLStats.Models.Location> Locations {get; set;} public LoLStats.Models.Location Location {get; set;} }
Index:
@model myNamespace.MyViewModel ... @foreach (var item in Model.Locations) { <ul> <li>@Html.DisplayFor(modelItem => item.Locations.LocationName)</li> </ul> ...
_createLocation:
@model myNamespace.MyViewModel ... <div class="editor-label"> @Html.LabelFor(model => model.Location.LocationName) </div> <div class="editor-field"> @Html.EditorFor(model => model.Location.LocationName) @Html.ValidationMessageFor(model => model.Location.LocationName) </div> ...
Hopefully that's enough detail to get you going
这篇关于如何使用@model IEnumerable的&LT;&GT;和@model&LT;&GT;在同一页面(列表/创建)MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!