开发者

MVC3 Razor > Make Model IEnumerable, so it can be used in View?

I have this model:

namespace easyBooking.Models
{
    public class CardInfo
    {
        public string name { get; set; }
        public string adress { get; set; }
        public string zipcode { get; set; }
        public string city { get; set; }
        public string ccsID { get; set; }
        public string birthday { get; set; }
        public string nationalitet { get; set; }
        public string cardType { get; set; }
    }
}

which I populate from a function and return to the controller. The problem is that I cannot hand this model to my view, since it does not implement IEnumerable... Instead I have to add information to the viewBag like this:

var cardInfo = FunctionLib.cardCampingPas(myArray);
                        ViewData.Add("name", cardInfo.name);
                        ViewData.Add("adress", cardInfo.adress);
                        ViewData.Add("nationalitet", cardInfo.nationalitet);
                        ViewData.Add("ccsID", cardInfo.ccsID);
                        ViewData.Add("zipcode", cardInfo.zipcode);
                        ViewData.Add("city", cardInfo.city);
                        ViewData.Add("cardType", cardInfo.cardType);
                        return View("../Reservation/New", ViewData);

which is kind of stupid, when I should be able to just add the cardInfo model directly to the View.

    var cardInfo = FunctionLib.cardCampingPas(myArray);
    return View("../Reservation/New", cardInfo);

So basically I just need to pass a bunch of strings to the view. How can my model CardInfo be accessed directly from my view?

Update

@rene - view data where I added a @model declaration. I did get some errors when I tried it last, but it must have been because I tried to foreach it or somet开发者_运维知识库hing.. now... just no error, and I have no idea if anything is passed to the view or how to get the data.

@model easyBooking.Models.CardInfo
@{
    ViewBag.Title = "New";
}
<h2>Ny reservation</h2>
<p>Benyttet kort: @Model.cardType</p>

This now works...


You can pass your collection directly to the view.

In the view, you'll need to declare the model type:

@model IEnumerable<CardInfo>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜