tags:

views:

54

answers:

2

Hi, I'm following this MVC tutorial and when I add a View for the Edit action, Model is null in the following snippet on the .aspx page:

<%= Html.TextBox("Id", Model.Id) %>

I'm learning MVC, so please understand if I'm doing a dumb thing. But as far as I can see, I've following the steps in the tutorial pretty well. And actually added the Create action and it works correctly.

Ideas appreciated.

A: 

Did you set the model in the controller? What does your controller method look like? Are you just returning View()? You need to pass the model as a parameter to that call like they do in the example:

return View(movieToEdit);

thekaido
+1  A: 

Is your view strongly typed?

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/TwoColumnUI.Master" Inherits="System.Web.Mvc.ViewPage<MyObject>" %>

then you would need to pass in an object of type MyObject from your controller action method

return View(new MyObject() { Id = 42 } );
hunter
Thank you, worked. public ActionResult Edit(int id) { return View(new Movies() { Id = id }); }
Ariel
Note: You don't HAVE to use the anonymous constructor.
hunter
This is not an anonymous constructor but an object initializer syntax in C# 3.0... And yes, I know it's not a necessary condition, thanks.
Ariel
tomato/ tomatoe
hunter