Reputation: 1060
First of all i'm quite new to the MVC 3 method, so please don't be to hard on me. I try to use model binding, with a model that has a List of another model. Attributes of this model succesfully bind, but the list doesn't.
Here are the classes:
public class Company
{
public int Id { get; set; }
public String Name { get; set; }
public List<Address> Addresses { get; set; }
}
public class Address
{
public int Id { get; set; }
public String Street { get; set; }
}
This is my model:
public class CompanyDetailsModel
{
public Classes.Company Company { get; set; }
public List<Address> Addressess { get; set; }
}
My controller (GET)
public ActionResult Create()
{
var model = new CompanyDetailsModel();
model.Addressess = new List<Address>();
//Just for this example like this
model.Addressess.Add(new Address());
model.Addressess.Add(new Address());
return View(model);
}
My controller (POST)
[HttpPost]
public ActionResult Create(CompanyDetailsModel CompanyDetailsModel)
{
foreach (var address in CompanyDetailsModel.Addressess)
{
//logic
}
return View();
}
View:
(also imported model of course)
in inside the form:
foreach (var address in Model.Addressess) { <fieldset> <legend>Test</legend> @Html.EditorFor(model => address.Street) </fieldset> }
Error : [NullReferenceException: Object reference not set to an instance of an object.]
Extra info: I can save all the attributes of company without any problems using the same CompanyDetailsModel.
I hope someone can help me out/point me in the right direction.
Thanks in advance
Gyo
Stack trace
Line 45: public ActionResult Create(CompanyDetailsModel CompanyDetailsModel)
Line 46: {
Line 47: foreach (var address in CompanyDetailsModel.Addressess)
Line 48: {
Line 49:
Source File: C:\Users\Goz\Documents\Visual Studio 2010\Projects\Portaal\Controllers\CompaniesController.cs Line: 47
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
Portaal.Controllers.CompaniesController.Create(CompanyDetailsModel CompanyDetailsModel) in C:\Users\Goz\Documents\Visual Studio 2010\Projects\Portaal\Controllers\CompaniesController.cs:47
lambda_method(Closure , ControllerBase , Object[] ) +108
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
System.Web.Mvc.Controller.ExecuteCore() +116
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8920029
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
Upvotes: 1
Views: 1395
Reputation: 1321
The reason why you get NullReferenceException
is incorrect list binding. Mvc has a few patterns in order to appropriate Collection binding. http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx here is good explanation of collection binding in MVC.
Upvotes: 0
Reputation: 33637
@Html.EditorFor(model => address.Street)
In creation of Model you have not specified Street value for address objects.
If this is not the case then please let us know the stack trace for the exception.
Upvotes: 1