【问题标题】:Posting a view that contains multiple partial views发布包含多个部分视图的视图
【发布时间】:2016-08-03 13:40:37
【问题描述】:

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

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

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

这是发布操作

[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,CModel类似于BModel的Partial view 注意:SetNumber 方法只创建一个新的 Object 并将其添加到列表中

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

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

【问题讨论】:

  • 你不能使用@Html.Partial(),除非你将主模型传递给方法,或者你指定HtmlFieldPrefix(参考this answer)。但正确的做法是为子模型使用EditorTemplate

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


【解决方案1】:

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

更新:

尝试替换你的

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

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

其中prefix 是模型属性名称(包含该部分模型,例如“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();
    }
}

模型视图:

@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 事件处理程序,它们将采用当前形式并发布到控制器上的不同操作。

【讨论】:

  • 这没有提供问题的答案。一旦你有足够的reputation,你就可以comment on any post;相反,provide answers that don't require clarification from the asker。 - From Review
  • 这部分回答了问题,至于更具体的答案需要更多细节。最常见的情况是缺少 HTML 标签的正确名称属性(在答案中提到),如果作者没有在他的代码中使用它们 - 这可能是一个问题。如果他使用这些名称 - 我们应该能够检查完整的代码以找到其他可能的问题。
  • 每个部分视图都有自己的模型 Project.Web.Models.PartialModel 所以我使用 Html.TextBoxItemFor(x => x.PartialID),我应该使用像这样的主模型 @model Project .Web.Models.MainView ?
  • 不,局部视图模型应该是该局部视图将用于的类型。如果此部分用于 BModel,则模型应为 BModel。我已经更新了我的回复,以便通过示例更清楚地说明问题。
  • 感谢它的工作! ^_^,我不再有 null,但部分视图中的 for 循环不起作用,我的 Dmodel 列表为空...
猜你喜欢
  • 1970-01-01
  • 2014-09-27
  • 1970-01-01
  • 1970-01-01
  • 2017-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-23
相关资源
最近更新 更多