2011-09-06 24 views
0

首先我对MVC 3方法很陌生,所以请不要对我很难。我尝试使用模型绑定,以及具有另一个模型列表的模型。此模型的属性成功绑定,但列表不。模型绑定不适用于我 - 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异常:对象没有设置的一个实例对象。]

额外信息:我可以保存公司的所有属性,没有任何问题,使用山姆e CompanyDetailsModel。

我希望有人能帮助我/指引我走向正确的方向。

在此先感谢

木桥

堆栈跟踪

Line 45:   public ActionResult Create(CompanyDetailsModel CompanyDetailsModel) 
Line 46:   { 
Line 47:    foreach (var address in CompanyDetailsModel.Addressess) 
Line 48:    { 
Line 49: 

源文件:C:\用户\戈兹贝达\文档\ Visual Studio 2010的\项目\ Portaal \控制器\ 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 

回答

1

@Html.EditorFor(model => address.Street)

在创建模型时,您尚未指定地址对象的街道值。

如果不是这种情况,请让我们知道异常的堆栈跟踪。

+0

我很抱歉,但我并不完全明白你的意思。你能解释一下吗?我不知道如果我解释好,但错误是在后控制器.. – Gyocol

+0

哦确定...你发布异常的堆栈跟踪? – Ankur

+0

是的,当然,我正在查找如何正确添加堆栈跟踪到我的问题...找不到它大声笑,在此先感谢 – Gyocol