2016-08-03 87 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

相關問題