发布包含多个部分视图的视图

时间:2016-08-03 13:40:38

标签: c# asp.net asp.net-mvc

我在发布包含多个部分视图的视图时遇到问题 使用一个提交按钮

这是我的A模型,

:更新:

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; }
}    

这是D模型

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

    public int? Number { get; set; }
}

这是c Model

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动作

[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>
}

我该怎么办?我错过了什么?

1 个答案:

答案 0 :(得分:0)

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

<强>更新

尝试更换

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

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

其中prefix是模型属性名称(持有该部分模型,例如x。&#34; BModel&#34;)。因此,您对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,那么这两个提交输入可以被一些按钮和onclick事件处理程序替换,这些处理程序将采用当前形式并发布到控制器上的不同操作。