简体   繁体   中英

MVC display records on view from controller without Viewbag/Viewdata

New to mvc, I have a controller like this

public ActionResult Index(DateTime Date, DateTime DDate)
{
DateTime dt = System.DateTime.Now.AddDays(5);
 ViewBag.b = dt;

 return View();
}

[HttpPost]
public ActionResult Index(DateTime Date, DateTime DDate)
{

List<myCollection> myCollectionList = new List<myCollection>();

// Fill the list

 return View();
}

Now, what I want to do is pass the myCollectionList to view (Razor cshtml) for displaying the collection records

Edited: myCollection class is in controller

public class myCollection
{
            public string date { get; set; }
            public string ddate { get; set; }
}

I'm not sure I understand correctly. You can simply return the myCollectionList from the action:

return View(myCollectionList);

The access it from the View using the Model property:

@model IList<myCollection>

...

@foreach (var obj in Model) {
  <li>@obj.Property</li>
}

See also this blog post .

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