Reputation: 932
I created a view that was working successfully with 1 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>
}
With a 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");
}
}
}
However I wanted to add another snippet like this on the same page, but with a different content path.
How do I add another model to this page?
I just had a controller and View, so I created a ViewModel creating 2 classes
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>
}
}
When I try to pass this into my view as @model IEnumerable<plantationmvc.Models.CommModel>
it says it does not exist in the namespace.
Upvotes: 8
Views: 2949
Reputation: 15727
You need any type for your model which has two collections in it.
It can be either your type (e.g. ArchivesViewModel
from the answer above) or you can even use 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);
}
View 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>
}
Upvotes: 2
Reputation: 3825
{
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
}
}
And add the view @model plantationmvc.Models.ArchivesViewModel
Upvotes: 8