2016-08-03 88 views
1

我在使用一个提交按钮发布包含

这是我的一个模型,张贴的其内具有 多个部分视图的视图的问题多局部视图的视图,

:更新时间:

public class AModel 
{ 
    public int AID { get; set; } 

    public int BID { get; set; } 

    public int CID { get; set; } 

    public int ANumber { get; set; } 

    public BModel BModel { get; set; } 

    public CModel CModel { get; set; } 
} 

这是我的B型

public class BModel 
{  
    public int BID { get; set; } 
    public int BName { get; set; } 

    public IList<DModel> DModel { get; set; } 
}  

THI s是d模型

public class DModel 
{ 
    public int DID { get; set; } 

    public int? Number { get; set; } 
} 

这是C型号

public class CModel 
{ 
    public int CID { get; private set; } 

    public IList<HModel> HModels { get; set; } 
} 

等 这是主视图

@model Project.Web.Models.AModel  
@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 
    @Html.Partial("CreateB",Model.BModel); 
    @Html.Partial("CreateC",Model.CModel); 
    <input style="border-radius:4px;width:100px;" type="button" value="next" id="next" class="btn btn-default" /> 
} 

这是获取动作

public ActionResult Create(){ 
Models.AModel model = new Models.AModel(){ 
BModel = new BModel(){ 
DModel = new List<Models.DModel>(){ new Models.DModel() }, 

CModel = new CModel(){ 
HModel = new List<HModel>(){ new Models.HModel() }, 
}; 
return View(model); 
} 

这是Post acti在

[HttpPost] 
public ActionResult Create(Models.AModel model) 
{ 
    //doing things with data 
    //when I reach this point all object are null 
    return RedirectToAction("index", model); 
} 

public void SetNumber(Models.BModel model){ 
model.DModel.Add(new Models.DModel()); 
} 

局部视图BModel和CModel类似于BModel局部视图 注:SetNumber方法只创建一个新的对象,并将其添加到列表中

@model Project.Web.Models.BModel 

@Html.TextBoxItemFor(x => x.BName) 

@for (int i=0; i< Model.DModel.Count; i++){ 
@Html.TextBoxItemFor(x => x.DModel[i].Number) 
<a id="addNumber" href="/AController/SetNumber/" data-ajax="true" data- 
ajax-method="GET" >Add Another Number</a> 
} 

我能做些什么?我错过了什么?

+0

除非您将主模型传递给该方法,否则您不能使用'@ Html.Partial()',或者指定'HtmlFieldPrefix'(请参阅[此答案](http://stackoverflow.com/questions/29808573/)得到最值-从-A-嵌套复杂对象 - 即-IS-传递到一个偏视图/ 29809907#29809907))。但正确的做法是为子模型使用'EditorTemplate' –

回答

0

很高兴看到您的部分以及。您应该记得将这些部分名称(作为主模型的属性)也放入输入的“name”属性中。因此,对于你的情况你PartialView应包含如下输入: <input name="AModel.BModel.BID" ... />

UPDATE:

尝试更换您的

@Html.Partial("Name", Model.PartialModel) 

@{ Html.RenderPartial("Name", Model.PartialModel, new ViewDataDictionary { TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = prefix } }); } 

其中prefix是Model属性名称(保留部分模型,例如“BModel”)。所以,你的AMODEL看法是这样的:

@model Project.Web.Models.AModel 
@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 
    @{ Html.RenderPartial("CreateB",Model.BModel, new ViewDataDictionary { TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "BModel" } }) } 
    @{ Html.RenderPartial("CreateC",Model.CModel, new ViewDataDictionary { TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "CModel" } }) 
    <input style="border-radius:4px;width:100px;" type="button" value="next" id="next" class="btn btn-default" /> 
} 

更新2:

为了使用数组(你在你的BModel有),你将需要修改这个前缀一点工作,所以鉴于您的BModel将包含:

@{ 
    for (var i = 0; i < Model.DModel.Count(); i++) 
    { 
     Html.RenderPartial("Name", Model.DModel[i], new ViewDataDictionary { TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "DModel["+i+"]" } }); 
    } 
} 

更新3:

下面是一个完整的例子(或者非常类似于你的情况),没有JavaScript。 控制器:

public ActionResult Test() 
    { 
     return View("Test1", new AModel()); 
    } 

    [HttpPost] 
    public ActionResult Save(AModel model) 
    { 
     if (model.PostType == "Add") 
     { 
      model.BModel.DModels.Add(new DModel()); 

      return View("Test1", model); 
     } 

     // Do something with this final model 

     return View("Test1"); 
    } 

型号:

public class DModel 
{ 
    public string Name { get; set; } 
} 

public class CModel 
{ 
    public string SomeName { get; set; } 
} 

public class BModel 
{ 
    public List<DModel> DModels { get; set; } 

    public BModel() 
    { 
     DModels = new List<DModel>(); 
    } 
} 

public class AModel 
{ 
    public BModel BModel { get; set; } 

    public CModel CModel { get; set; } 

    public string PostType { get; set; } 

    public AModel() 
    { 
     BModel = new BModel(); 
     CModel = new CModel(); 
    } 
} 

AMODEL查看:

@model MVC.Models.AModel 
@if (Model == null) 
{ 
    <span>Model saved</span> 
} 
else 
{ 
    using (Html.BeginForm("Save", "Home", FormMethod.Post)) 
    { 
     Html.RenderPartial("Partials/CModel", Model.CModel, new ViewDataDictionary { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "CModel" } }); 
     <hr /> 
     Html.RenderPartial("Partials/BModel", Model.BModel, new ViewDataDictionary { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "BModel" } }); 

     <input type="submit" name="PostType" value="Add"/><br /> 
     <input type="submit" name="PostType" value="Save"/> 
    } 
} 

BModel管窥:

@model MVC.Models.BModel 

@{ 
    for (var i = 0; i < Model.DModels.Count(); i++) 
    { 
     Html.RenderPartial("Partials/DModel", Model.DModels[i], new ViewDataDictionary { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = $"{ViewData.TemplateInfo.HtmlFieldPrefix}.DModels[{i}]" } }); 
    } 
} 

DModel管窥:

@model MVC.Models.DModel 

Name: @Html.EditorFor(x => x.Name) <br/> 

CModel管窥:

@model MVC.Models.CModel 

SomeName: @Html.EditorFor(x => x.SomeName) <br/> 

如果你可以使用jQuery那么那些2提交的投入可以用一些按钮和onclick事件处理程序,将采取目前的形式,并张贴到控制器上的不同的动作来代替。

+0

这不能提供问题的答案。一旦你有足够的[声誉](http://stackoverflow.com/help/whats-reputation),你将能够[评论任何职位](http://stackoverflow.com/help/privileges/comment);相反,[提供不需要提问者澄清的答案](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [来自评论](/ review/low-quality-posts/13213623) –

+0

这部分回答问题,因为更具体的答案需要更多的细节。最常见的情况是缺少HTML标签的正确名称属性(在答案中提到),如果作者没有在他的代码中使用它们 - 这可能是一个问题。如果他使用这些名称 - 我们应该能够检查完整的代码以查找其他可能的问题。 –

+0

每个部分视图都有它自己的模型Project.Web.Models.PartialModel,所以我使用Html.TextBoxItemFor(x => x.PartialID),我应该像这样使用主模型@model Project.Web.Models.MainView? – Lucinda

相关问题