Прежде всего, я новичок в методе MVC 3, поэтому, пожалуйста, не обращайте на меня внимания. Я пытаюсь использовать привязку модели с моделью, у которой есть Список другой модели. Атрибуты этой модели успешно связываются, а список нет.
Вот классы:
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; }
}
Это моя модель:
public class CompanyDetailsModel
{
public Classes.Company Company { get; set; }
public List<Address> Addressess { get; set; }
}
Мой контроллер (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);
}
Мой контроллер (POST)
[HttpPost]
public ActionResult Create(CompanyDetailsModel CompanyDetailsModel)
{
foreach (var address in CompanyDetailsModel.Addressess)
{
//logic
}
return View();
}
Вид:
(тоже импортная модель конечно)
внутри формы:
foreach (var address in Model.Addressess)
{
<fieldset>
<legend>Test</legend>
@Html.EditorFor(model => address.Street)
</fieldset>
}
Ошибка:
[NullReferenceException: ссылка на объект не установлена для экземпляра объекта.]
Дополнительная информация: я могу без проблем сохранить все атрибуты компании, используя ту же CompanyDetailsModel.
Я надеюсь, что кто-то может помочь мне / направить меня в правильном направлении.
Заранее спасибо
Г
трассировка стека
Line 45: public ActionResult Create(CompanyDetailsModel CompanyDetailsModel)
Line 46: {
Line 47: foreach (var address in CompanyDetailsModel.Addressess)
Line 48: {
Line 49:
Исходный файл: C: \ Users \ Goz \ Documents \ Visual Studio 2010 \ Projects \ Portaal \ Controllers \ CompaniesController.cs Строка: 47
Трассировка стека:
[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