Search code examples
c#asp.netasp.net-mvcdnx

Asp.net MVC binding to view model


Current Situation: Starting with ASP.net MVC development and coming from MVVM app development I'm struggling with the binding of a view with view model / controller. I started with an empty project and tried to create model, viewmodel, controller and view. Starting the project I get a "500 Internal server error" but don't understand what's wrong (no error in the output window). I just can't understand how a view actually binds to a view model (probably because I think too much in MVVM).

What I currently have:

Startup.cs:

using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace WebApplication1
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app)
        {
            app.UseIISPlatformHandler();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Sample}/{action=Index}/{id?}");
            });
        }

        // Entry point for the application.
        public static void Main(string[] args) => WebApplication.Run<Startup>(args);
    }
}

Model:

using System;

namespace WebApplication1.Models
{
    public class SomeModel
    {
        public string Title { get; set; }
        public DateTime Time { get; set; }
    }
}

ViewModel:

using System;
using System.Collections.Generic;
using WebApplication1.Models;

namespace WebApplication1.ViewModels
{
    public class SampleViewModel
    {
        public IList<SomeModel> SomeModels { get; set; }

        public SampleViewModel()
        {
            SomeModels = new List<SomeModel>();
            SomeModels.Add(new SomeModel()
            {
                Time = DateTime.Now,
                Title = "Hallo"
            });
        }
    }
}

Controller:

using Microsoft.AspNet.Mvc;
using WebApplication1.ViewModels;

namespace WebApplication1.Controllers
{
    public class SampleController : Controller
    {
        //
        // GET: /Sample/
        public IActionResult Index()
        {
            return View(new SampleViewModel());
        }
    }
}

View:

@model WebApplication1.ViewModels.SampleViewModel

<!DOCTYPE html>
<html>
<head>
    <title>Hallo</title>
</head>
<body>
    @foreach (var someModel in Model.SomeModels)
    {
        <div>@someModel.Title</div>
    }
</body>
</html>

I found a lot of articles talking about model binding but they only talk about forms and input. What I want is to show some data, e.g. from a database in some kind of list or so and therefore don't need to post any date to the website.

Does anyone see the (probably obvious) issue in my sample code?

The project is based on ASP.net 5 MVC 6 using DNX.

I already set some breakpoints to see whether the controller is actually called. And it is. I went through the few methods with the debugger without any issue. Also, the output window does not show any error or sth. like that.


Solution

  • The view name was missing in the result for the GET method. So instead of

    return View(new SampleViewModel()); 
    

    it must be

    return View("Sample", new SampleViewModel());
    

    I thought that the connection between view and controller is purely convention based. So a controller named Sample searches for a view named Sample in a folder called Sample which in turn is a subfolder of Views.

    Not sure exactly why, but it works this way.