如何使用封装类模型从两次出现的相同局部视图中获取数据

时间:2015-02-24 17:29:33

标签: asp.net-mvc-5 asp.net-mvc-partialview

我有一个自动填充器作为局部视图,我想在同一页面中使用两次。涉及三个模型

public partial class CustomParent
{

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Int64 CustomParentId { get; set; }

    public Geoname ParentGeoname { get; set; }
    public Geoname ChildGeoname { get; set; }

    public CustomParent()
    {
        ParentGeoname = new Geoname();
        ChildGeoname = new Geoname();
    }
}
public partial class Geoname
{
    [Required]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Int64 GeonameId { get; set; }
    public string Name { get; set; }
}
public partial class GeonameWithFilter
{
    public GeonameWithFilter()
    {
        FilterString = "";
    }
    public Geoname Geoname { get; set; }
    public String FilterString { get; set; }
}

我有一个控制器设置

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "CustomParentId,ParentGeoname,ChildGeoname")] CustomParent customParent)
    {...}

我最初只使用customparent和geoname模型在我的创建视图上设置了两个Html部分,并且可以正常设置父和子geonames的值。我现在需要将其他参数传递给局部视图,因此我创建了一个封装类(GeonameWithFilter)。我对部分视图和视图页面上的两个html.partials进行了更改,现在看起来像这样:

            @Html.Partial("_GeonameAutocomplete",new GeonameWithFilter(){Geoname = Model.ParentGeoname,FilterString="'featurecodes':'CUPA'"},
                new ViewDataDictionary()
                {
                    TemplateInfo = new TemplateInfo() { HtmlFieldPrefix = "ParentGeoname" }
                })
            @Html.Partial("_GeonameAutocomplete", new GeonameWithFilter() { Geoname = Model.ChildGeoname, FilterString = "'featurecodes':'ADM1,ADM2,ADM3,ADM4'" },
                new ViewDataDictionary()
                {
                    TemplateInfo = new TemplateInfo() { HtmlFieldPrefix = "ChildGeoname" }
                })

问题是customparent.parentGeoname和customparent.childGeoname现在没有返回到我的控制器。我猜这是因为部分视图的模型与我的页面模型parentGeoname和childGeoname不是同一个类,但如果确实可能的话,无法解决或找到如何处理这种情况的任何示例。

1 个答案:

答案 0 :(得分:0)

嗯 - 它花了我一天的大部分时间,但我现在有了我所需要的。

我放弃了封装,而是将我的基本geoname模型添加到新的ViewDataDictionary中,并取消了Html.Partial的默认模型。

然后我在每个ViewDataDictionary中添加了filterstring参数作为键,我将其用作模型和TemplateInfo的参数。

非常感谢https://stackoverflow.com/users/7714/craig-stuntz回答了另一个问题Shorthand for creating a ViewDataDictionary with both a model and ViewData items?,这个问题指出了我的方向。

我的自动填充功能现在只需使用@ViewData [" FilterString"]来访问过滤器参数。不再需要GeonameWithFilter封装。我视图页面上的两个Html.Partials现在看起来像这样:

            @Html.Partial("_GeonameAutocomplete", null,
                new ViewDataDictionary(new ViewDataDictionary() { {"FilterString",  "featurecodes:'CUPA'"  }})
                {
                    Model = Model.ParentGeoname,
                    TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "ParentGeoname" }
                })
            @Html.Partial("_GeonameAutocomplete", null,
                new ViewDataDictionary(new ViewDataDictionary() { { "FilterString", "featurecodes:'ADM1,ADM2,ADM3,ADM4'" } })
                {
                    Model = Model.ChildGeoname,
                    TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "ChildGeoname" }
                })

如果有人知道更好的方法来达到最终结果,我仍然希望听到它。