Regarding return view with model usage

Thomas

i am new in mvc. i have got a code where view is returning with model. what does it mean. if i return view with model then model data can be access from view.

public ActionResult GetView(int id)
{
    switch (id)
    {
        case 1:
            return View("View1", model1);
            break;
        case 2:
            return View("View2", model2);
            break;
        default:
            return View("Default", modelDefault);
    }
}

i need to see a small complete example where view will be return with model and view will be populated with model data. can anyone redirect me to a good example on this topic. thanks

Claudio Redi

Here you have a sample

CONTROLLER

public ActionResult Index()
{
    // return a list of movies to view
    return View(db.Movies.ToList());
}

VIEW

//declare expected type of model, if view returns something else, it will fail
@model IEnumerable<MvcMovie.Models.Movie> 

//use model sent from controller
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ReleaseDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Genre)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Price)
        </td>
         <th>
            @Html.DisplayFor(modelItem => item.Rating)
        </th>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", { id=item.ID })  |
            @Html.ActionLink("Delete", "Delete", { id=item.ID }) 
        </td>
    </tr>
}

More information on Accessing Your Model's Data from a Controller

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事