在1个视图中添加2个IEnumerable模型

时间:2015-08-06 21:23:05

标签: c# razor model-view-controller

我创建了一个使用1个视图成功运行的视图

@model IEnumerable<string>
<ul>
    @foreach (var fName in Model)
    {
        var name = fName;
        var link = @Url.Content("~/Content/archives/mgamm/") + name.Replace(" ", "%20");

        <li style="list-style:none; font-size:1.2em;">
            <a href="@link">@name</a>
        </li>
    }
</ul>
@if (User.IsInRole("admin"))
{
    <div>
        @using (Html.BeginForm("Index", "Archives", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <input type="File" name="file" id="file" value="Choose File" />
            <button type="submit">Upload</button>
        }
    </div>
}

使用控制器

namespace plantationmvc.Controllers
{
    public class ArchivesController : Controller
    {
        //
        // GET: /Archives/
        public ActionResult Index()
        {
            var path = Server.MapPath("~/Content/archives/mgamm");

            var dir = new DirectoryInfo(path);

            var files = dir.EnumerateFiles().Select(f => f.Name);

            return View(files);
        }

        [HttpPost]
        public ActionResult Index(HttpPostedFileBase file)
        {
            var path = Path.Combine(Server.MapPath("~/Content/archives/mgamm"), file.FileName);

            var data = new byte[file.ContentLength];
            file.InputStream.Read(data, 0, file.ContentLength);

            using (var sw = new FileStream(path, FileMode.Create))
            {
                sw.Write(data, 0, data.Length);
            }

            return RedirectToAction("Index");
        }
    }
}

但是我想在同一页面上添加这样的另一个代码段,但内容路径不同。

如何在此页面中添加其他模型?

我只有一个控制器和View,所以我创建了一个创建2个类的ViewModel

namespace plantationmvc.Models
{
    public class ArchivesViewModel
    {
        public CommModel Model1 { get; set; }
        public MeetModel Model2 { get; set; }
    }

    public class CommModel
    {
        public IEnumerable<CommModel>              
    }
    public class MeetModel 
    {
        public IEnumerable<MeetModel>
    }
}

当我尝试将其作为@model IEnumerable<plantationmvc.Models.CommModel>传递给我的视图时,它表示它在命名空间中不存在。

2 个答案:

答案 0 :(得分:7)

{
 public class ArchivesViewModel
 {
    public IEnumerable<CommModel> Model1 { get; set; }
    public IEnumerable<MeetModel> Model2 { get; set; }
 }

 public class CommModel
 {
    //properties of CommModel 

 }
 public class MeetModel 
 {
   //properties of Meet Model
 }

 }

添加视图@model plantationmvc.Models.ArchivesViewModel

答案 1 :(得分:2)

您的模型需要任何类型,其中包含两个集合。

它可以是您的类型(例如上面的答案中的ArchivesViewModel),也可以使用Tuple<T1, T2>

的Controler:

public ActionResult Index()
{
    var list1 = new[] { "1", "2", "3", "4", "5" };
    var list2 = new[] { "10", "20", "30", "40", "50" };
    var model = Tuple.Create<IEnumerable<string>, IEnumerable<string>>(list1, list2);
    return View(model);
}

查看Index.schtml

@model Tuple<IEnumerable<string>, IEnumerable<string>>

@foreach (var a in Model.Item1)
{
    <h2>@a</h2> 
}
@foreach (var b in Model.Item2)
{ 
    <h3>@b</h3>
}