如何使用@model IEnumerable的<>和@model<>在同一页面(列表/创建)MVC3 [英] How to use @model IEnumerable<> and @model <> in the same page (List / Create) MVC3

查看:268
本文介绍了如何使用@model IEnumerable的<>和@model<>在同一页面(列表/创建)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的< LoLStats.Models.Location>@ {
    ViewBag.Title =指数;
}
@foreach(以型号VAR项){
    < UL>
        <立GT; @ Html.DisplayFor(modelItem => item.LocationName)LT; /李>
    < / UL>
}<一类=BTN数据切换=模态的href =#myModal>创建< / A>< D​​IV CLASS =模式隐藏ID =myModal>
  < D​​IV CLASS =模头>
    <按钮式=按钮级=关闭数据解雇=模式>&×LT; /按钮>
    < H3>模态头< / H3 GT&;
  < / DIV>
  < D​​IV CLASS =模体>
    &所述p为H.; @ Html.Partial(_ createLocation)&所述; / P>
  < / DIV>
  < D​​IV CLASS =模式躯>
    < A HREF =#类=BTN数据解雇=模式>关闭< / A>
    < A HREF =#类=BTN BTN-小学>保存更改LT; / A>
  < / DIV>
< / DIV>

_createLocation:


  

@model LoLStats.Models.Location


  
  

@ {
      ViewBag.Title =创建; }


  
  

@using(Html.BeginForm()){
      @ Html.ValidationSummary(真)
      
          位置

 < D​​IV CLASS =编辑标记>
        @ Html.LabelFor(型号=> model.LocationName)
    < / DIV>
    < D​​IV CLASS =主编场>
        @ Html.EditorFor(型号=> model.LocationName)
        @ Html.ValidationMessageFor(型号=> model.LocationName)
    < / DIV>
    &所述p为H.;
        <输入类型=提交值=创建/>
    &所述; / P>
< /字段集> }



解决方案

您可以让一个具有两个属性视图模型:


  • 了IEnumerable父视图

  • 的模式一个独具特色的对象

然后强类型既欣赏到视图模型,你应该准备就绪。

修改

视图模型:

 公共类MyViewModel()
{
  公共IEnumerable的< LoLStats.Models.Location>位置{搞定;组;}
  公共LoLStats.Models.Location位置{搞定;组;}
}

指数:

  @model myNamespace.MyViewModel
...
@foreach(在Model.Locations VAR项){
    < UL>
        <立GT; @ Html.DisplayFor(modelItem => item.Locations.LocationName)LT; /李>
    < / UL>
...

_createLocation:

  @model myNamespace.MyViewModel
...
< D​​IV CLASS =编辑标记>
        @ Html.LabelFor(型号=> model.Location.LocationName)
    < / DIV>
    < D​​IV CLASS =主编场>
        @ Html.EditorFor(型号=> model.Location.LocationName)
        @ Html.ValidationMessageFor(型号=> model.Location.LocationName)
    < / DIV>
...

我希望这是足够的细节,让你去

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆