Asp.net mvc 2 Post方法asp.net mvc 2中的数据为空

Asp.net mvc 2 Post方法asp.net mvc 2中的数据为空,asp.net-mvc-2,Asp.net Mvc 2,我试图显示具有某种自定义类类型属性的类。 型号: 控制器: public ActionResult Testify(int id) { ComplexParent par = new ComplexParent(); par.First = new SimpleChild() { Id = id }; par.Second = new SimpleChild() { Id = id + 1,

我试图显示具有某种自定义类类型属性的类。 型号:

控制器:

public ActionResult Testify(int id)
    {
        ComplexParent par = new ComplexParent();
        par.First = new SimpleChild() { Id = id };
        par.Second = new SimpleChild()
        {
            Id = id + 1,
            ChildName = "Bob",
            ChildDescription = "Second"
        };

        return View("Testify", par);
    }

    [HttpPost]
    public ActionResult Testify(ComplexParent pComplexParent)
    {

        return View("Testify", pComplexParent);
    }
[HandleError]
public class HomeController : Controller
{
    public ActionResult Testify(int id)
    {
        var par = new ComplexParent();
        par.First = new SimpleChild() { Id = id };
        par.Second = new SimpleChild()
        {
            Id = id + 1,
            ChildName = "Bob",
            ChildDescription = "Second"
        };

        return View(par);
    }

    [HttpPost]
    public ActionResult Testify(ComplexParent pComplexParent)
    {
        return View(pComplexParent);
    }
}
视图:


领域
x、 第一)%>

x、 第二,ChildName)%%>


当它工作正常时,我可以看到所有的数据。但在post pComplexParent上,参数为空(复杂类的两个属性都为空)。也许我在这里遗漏了一些东西,但无法让它工作。。。 小添加:只显示名称编辑器的视图部分使第二个子项不为null,并且名称设置为Bob。但是我不明白如何只用EditorFor或DisplayFor方法来实现它


更新:感谢Darin Dimitrov,他仔细检查了我的所有代码,发现了导致此问题的原因。确切的问题是,如果您使用的是显示模板,asp.net mvc 2不会发回任何值,并且如果整个模板没有任何内容可发回,则对象为null。我仍在考虑如何获取数据,即使你不想编辑数据。但是使用编辑器模板可以做到这一点,我现在已经用正确的数据填充了所有对象。

您的视图有点乱。您正在为第一个子项使用编辑器模板和部分。表单中包含哪些字段并不十分清楚。我建议您仅使用编辑器模板:

型号:

public class ComplexParent
{
    public SimpleChild First { get; set; }
    public SimpleChild Second { get; set; }
}
public class SimpleChild
{
    public int Id { get; set; }
    public string ChildName { get; set; }
    public string ChildDescription { get; set; }
}
控制器:

public ActionResult Testify(int id)
    {
        ComplexParent par = new ComplexParent();
        par.First = new SimpleChild() { Id = id };
        par.Second = new SimpleChild()
        {
            Id = id + 1,
            ChildName = "Bob",
            ChildDescription = "Second"
        };

        return View("Testify", par);
    }

    [HttpPost]
    public ActionResult Testify(ComplexParent pComplexParent)
    {

        return View("Testify", pComplexParent);
    }
[HandleError]
public class HomeController : Controller
{
    public ActionResult Testify(int id)
    {
        var par = new ComplexParent();
        par.First = new SimpleChild() { Id = id };
        par.Second = new SimpleChild()
        {
            Id = id + 1,
            ChildName = "Bob",
            ChildDescription = "Second"
        };

        return View(par);
    }

    [HttpPost]
    public ActionResult Testify(ComplexParent pComplexParent)
    {
        return View(pComplexParent);
    }
}
视图:

现在,如果要为两个子属性使用不同的编辑器模板,可以在包含编辑器模板时指定编辑器模板名称:

<%: Html.EditorFor(x => x.First, "FirstChildEditor") %>

我的建议是不要使用
Html.RenderPartial
来生成输入字段,因为它们的名称将被硬编码,并且不会根据对象层次结构进行正确绑定。

提示-使用Fiddler查看HTTP POST请求,以查看发送了哪些POST数据。查看键/值对是否与您的模型匹配,因为这正是MVC试图做的(显然是失败的)只是使用请求对象进行检查,到目前为止还没有任何结果。这就是我的观点。检查Fiddler中的
原始HTTP数据包
。如果它与模型不匹配,它将为空。好的,现在执行。假设它不匹配。我做错了什么,它不匹配?我认为asp.NETMVC2应该解析表单数据并执行适当的映射,不是吗?实际上以前没有使用fiddler。无法了解如何分析本地主机流量。不过我认为这个问题应该以一种简单的方式解决,因为asp.NETMVC2应该执行映射。达林,谢谢你们的帮助。是的,一切都一团糟。我刚刚创建了一个测试样本,并试图让它工作。因此,不同的方法(如EditorFor和RenderPartial)会产生不同的结果。它不是生产代码,只是我的测试模型、视图和控制器。
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SomeNs.Models.SimpleChild>" %>

<%: Html.HiddenFor(x => x.Id) %>
<%: Html.EditorFor(x => x.ChildName) %>
<%: Html.EditorFor(x => x.ChildDescription) %>
<%: Html.EditorFor(x => x.First, "FirstChildEditor") %>
public class ComplexParent
{
    [UIHint("FirstChildEditor")]
    public SimpleChild First { get; set; }
    public SimpleChild Second { get; set; }
}