简体   繁体   English

一个视图中有两个模型类型

[英]Two model type in one View

I have a concern. 我很担心 I am populating a list from controller and showing a table. 我从控制器填充列表并显示表格。 Now upon clicking of a button in table row, i want to populate modal popup for webform, which is a partial view. 现在,单击表行中的按钮后,我想为Webform填充模式弹出窗口,这是局部视图。 My problem is, when i am using List Type model, i am not able to convert that model from list to simple as i have to pass simple model to render webform. 我的问题是,当我使用列表类型模型时,我无法将该模型从列表转换为简单模型,因为我必须通过简单模型来呈现Webform。 My code is as below: 我的代码如下:

Index.cshtml Index.cshtml

@model List<MvcApplication3.Models.ModelList>
//////
////
@foreach (var item in Model)
{
    // foreach is used to show data in table
    @Html.Parial("PartialView")
}

ModelList.cs 型号清单

public class ModelList
{
    public string Name { set; get; }
    public string Product { set; get; }
    public string Status { set; get; }
    public string Password { set; get; }
    public string Email { set; get; }
}

PartialView.cshtml PartialView.cshtml

@model MvcApplication3.Models.ModelList
@using (Html.BeginForm())
{
////
}

Controller 控制者

List<ModelList> objList = new List<ModelList>();
return View(objList);

How can i be able to use same model in both ways. 我如何能够以两种方式使用相同的模型。

@foreach (var item in Model)
{
    // foreach is used to show data in table
    @Html.Partial("PartialView", item)
}

You need to tell it to use your item in the partial 您需要告诉它在局部使用项目

try to use IEnumeraible in your index.cshtml view and then loop it and pass every new instance of ModelList to PartialView like this: 尝试在index.cshtml视图中使用IEnumeraible,然后对其进行循环,并将ModelList的每个新实例传递给PartialView,如下所示:

index.cshtml index.cshtml

@model IEnumeraible<MvcApplication3.Models.ModelList>

@foreach (var item in Model)
{
    @Html.Parial("PartialView", item)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM