c# - 在 1 个 View 中添加 2 个 IEnumerable 模型

标签 c# razor model-view-controller

我创建了一个在 1 个 View 中成功运行的 View

@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>
}

有 Controller

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");
        }
    }
}

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

如何向该页面添加另一个模型?

我只有一个 Controller 和一个 View ,所以我创建了一个 ViewModel,创建了 2 个类

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> 传递到我的 View 中时它说它不存在于命名空间中。

最佳答案

{
 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
 }

 }

并添加 View @model plantationmvc.Models.ArchivesViewModel

关于c# - 在 1 个 View 中添加 2 个 IEnumerable 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31866049/

相关文章:

c# - 使用 linq 时从 WCF 服务返回什么?

jquery - 在 mvc3 jquery 中将下拉选定的值获取到文本框

javascript - Ember JS - 无法将多个模型加载到具有 belongsTo 关系的路由中

model-view-controller - 快速保存当前 View Controller

java - 使用复杂模型且无需类型检查和 if-else 链来实现 MVC 的正确方法

c# - 如何创建一个新的 FlatButtonAppearance 对象?

C# 随机选择单词不起作用

javascript - ajax 以对象作为参数调用 Controller 操作

c# - 将 <text> 转换为字符串?

javascript - MVC 3 Razor View : Generating JavaScript from a boolean model value