C# 发布包含多个局部视图的视图

C# 发布包含多个局部视图的视图,c#,asp.net,asp.net-mvc,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; }

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

这是我的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; }
}    
这是c模型

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

    public IList<HModel> HModels { get; set; }
}
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>
}
@model Project.Web.Models.BModel
@Html.TextBoxItemFor(x=>x.BName)
@对于(int i=0;ix.DModel[i].Number)
}

我能做什么?我遗漏了什么?

如果能看到你的部分,我会很高兴的。您应该记住将这些部分名称(作为主模型的属性)也放在输入的“name”属性中。因此,对于您的案例,您的PartialView应该包含如下输入:

更新:

试着换掉你的衣服

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

其中,
前缀
是模型属性名称(包含该部分模型,例如“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" />
}
型号:

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();
    }
}
公共类数据模型
{
公共字符串名称{get;set;}
}
公共类CModel
{
公共字符串SomeName{get;set;}
}
公共类B模型
{
公共列表数据模型{get;set;}
公共BModel()
{
DModels=新列表();
}
}
公共类模型
{
公共BModel BModel{get;set;}
公共CModel CModel{get;set;}
公共字符串PostType{get;set;}
公共模型()
{
BModel=新的BModel();
CModel=新的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"/>
    }
}
@model MVC.Models.AModel
@if(Model==null)
{
模型保存
}
其他的
{
使用(Html.BeginForm(“Save”、“Home”、FormMethod.Post))
{
RenderPartial(“Partials/CModel”,Model.CModel,newviewDataDictionary{TemplateInfo=newtemplateInfo{HtmlFieldPrefix=“CModel”});

RenderPartial(“Partials/BModel”,Model.BModel,newviewDataDictionary{TemplateInfo=newtemplateInfo{HtmlFieldPrefix=“BModel”});
} }
b模型局部视图:

@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}]" } });
    }
}
@model MVC.Models.DModel

Name: @Html.EditorFor(x => x.Name) <br/>
@model MVC.Models.CModel

SomeName: @Html.EditorFor(x => x.SomeName) <br/>
@model MVC.Models.BModel
@{
对于(var i=0;i
D模型局部视图:

@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}]" } });
    }
}
@model MVC.Models.DModel

Name: @Html.EditorFor(x => x.Name) <br/>
@model MVC.Models.CModel

SomeName: @Html.EditorFor(x => x.SomeName) <br/>
@model MVC.Models.DModel
名称:@Html.EditorFor(x=>x.Name)
CModel局部视图:

@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}]" } });
    }
}
@model MVC.Models.DModel

Name: @Html.EditorFor(x => x.Name) <br/>
@model MVC.Models.CModel

SomeName: @Html.EditorFor(x => x.SomeName) <br/>
@model MVC.Models.CModel
SomeName:@Html.EditorFor(x=>x.SomeName)

若您可以使用jQuery,那个么这两个提交输入可以被一些按钮和
onclick
事件处理程序所取代,这些事件处理程序将采用当前形式并发布到控制器上的不同操作

也很高兴看到您的片段。您应该记住将这些部分名称(作为主模型的属性)也放在输入的“name”属性中。因此,对于您的案例,您的PartialView应该包含如下输入:

更新:

试着换掉你的衣服

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

其中,
前缀
是模型属性名称(包含该部分模型,例如“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" />
}
型号:

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();
    }
}
公共类数据模型
{
公共字符串名称{get;set;}
}
公共类CModel
{
公共字符串SomeName{get;set;}
}
公共类B模型
{
公共列表数据模型{get;set;}
公共BModel()
{
DModels=新列表();
}
}
公共类模型
{
公共BModel BModel{get;set;}
公共CModel CModel{get;set;}
公共字符串PostType{get;set;}
公共模型()
{
BModel=新的BModel();
CModel=新的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"/>
    }
}
@model MVC.Models.AModel
@if(Model==null)
{
模型保存
}
其他的
{
使用(Html.BeginForm(“Save”、“Home”、FormMethod.Post))
{
RenderPartial(“Partials/CModel”,Model.CModel,newviewDataDictionary{TemplateInfo=newtemplateInfo{HtmlFieldPrefix=“CModel”});

RenderPartial(“Partials/BModel”,Model.BModel,newviewDataDictionary{TemplateInfo=newtemplateInfo{HtmlFieldPrefix=“BModel”});
} }
b模型局部视图:

@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}]" } });
    }
}
@model MVC.Models.DModel

Name: @Html.EditorFor(x => x.Name) <br/>
@model MVC.Models.CModel

SomeName: @Html.EditorFor(x => x.SomeName) <br/>
@model MVC.Models.BModel
@{
对于(var i=0;i
D模型局部视图:

@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}]" } });
    }
}
@model MVC.Models.DModel

Name: @Html.EditorFor(x => x.Name) <br/>
@model MVC.Models.CModel

SomeName: @Html.EditorFor(x => x.SomeName) <br/>
@model MVC.Models.DModel
名称:@Html.EditorFor(x=>x.Name)
CModel局部视图:

@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}]" } });
    }
}
@model MVC.Models.DModel

Name: @Html.EditorFor(x => x.Name) <br/>
@model MVC.Models.CModel

SomeName: @Html.EditorFor(x => x.SomeName) <br/>
@model MVC.Models.CModel
SomeName:@Html.EditorFor(x=>x.SomeName)

若您可以使用jQuery,那个么这两个提交输入可以被一些按钮和
onclick
事件处理程序所取代,这些事件处理程序将采用当前形式并发布到控制器上的不同操作

这并不能回答这个问题。一旦你有足够的钱,你将能够;相反这部分回答了问题,因为更具体的回答需要更多的细节。最常见的情况是缺少HTML标记的正确名称属性(答案中提到过),如果作者在代码中不使用它们,这可能是一个问题。如果他使用这些名称-我们应该能够检查完整的代码以找到其他可能的问题。每个局部视图都有自己的模型Project.Web.Models.PartialModel,所以我使用的是Html.TextBoxItemFor(x=>x.PartialID),我应该使用像@model Project.Web.Models.MainView这样的主模型吗?不,局部视图模型应为该局部视图将用于的类型。如果此部分用于BModel,则模型应为BModel。我已经更新了对的回复