简体   繁体   中英

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. 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. My code is as below:

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

@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

@model IEnumeraible<MvcApplication3.Models.ModelList>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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