简体   繁体   English

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

[英]Posting a view that contains multiple partial views

I'm having a problem with posting a view that has multiple partial views within it using one submit button 我在使用一个提交按钮发布包含多个部分视图的视图时遇到问题

This is my A model, 这是我的A型号,

:UPDATED: :更新:

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

this is my B model 这是我的B型号

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

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

this is the D model 这是D模型

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

    public int? Number { get; set; }
}

this is the c Model 这是c模型

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

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

and so on this is the main view 等等这是主要观点

@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" />
}

this is the Get action 这是Get动作

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

this is the Post action 这是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());
}

Partial View For BModel, and CModel is similar to BModel Partial view Note: the SetNumber method only create a new Object and add it to the list 部分视图对于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>
}

What can I do? 我能做什么? What Am I missing? 我错过了什么?

Would be nice to see your partials as well. 很高兴看到你的部分。 You should remember to put those partials names (as properties from main model) to "name" attribute of input as well. 您应该记住将这些部分名称(作为主模型的属性)也放到输入的“名称”属性中。 So for your case your PartialView should contain inputs like these: <input name="AModel.BModel.BID" ... /> 因此,对于您的情况,您的PartialView应包含以下输入: <input name="AModel.BModel.BID" ... />

UPDATE: 更新:

Try to replace your 尝试更换你的

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

to

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

where prefix is Model property name (holding that partial model, ex "BModel"). 其中prefix是Model属性名称(持有该部分模型,ex“BModel”)。 So your view for AModel would look like this: 因此,您对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" />
}

UPDATE 2: 更新2:

In order to work with arrays (which you have in your BModel) you will need to modify this prefix a bit, so view for your BModel would contain: 为了使用数组(您在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+"]" } });
    }
}

UPDATE 3: 更新3:

Here's a complete example for your case (or pretty similar to your) without JavaScript. 这是一个完整的例子,适用于没有JavaScript的情况(或非常类似于你)。 Controller: 控制器:

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

Models: 楷模:

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 View: 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 Partial View: 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 Partial View: DModel局部视图:

@model MVC.Models.DModel

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

CModel Partial View: CModel局部视图:

@model MVC.Models.CModel

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

If you can use jQuery then those 2 submit inputs could be replaced with some buttons and onclick event handlers which will take current form and post to different actions on controller. 如果你可以使用jQuery,那么这两个提交输入可以被一些按钮和onclick事件处理程序替换,这些处理程序将采用当前形式并发布到控制器上的不同操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM