0

我有一个outerViewModel,里面有两个ViewModel,当我尝试绑定innermodel时,所有属性都为null...

这是代码:

**Models.cs**

public class OuterModel
{
    public FirstInnerModel firstInnerModel;
    public SecondInnerModel secondInnerModel;
}

public class FirstInnerModel
{
    public string Title;
}

public class SecondInnerModel
{
    public string Title;
}

索引.cshtml

            @using (Html.BeginForm("ActivateFirst", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {

               @Html.ValidationSummary(true)

                <fieldset>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.firstInnerModel.Title)
                    </div>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.firstInnerModel.Title)
                        @Html.ValidationMessageFor(model =>          model.firstInnerModel.Title)
                    </div>
                    <p>
                       <input type="submit" value="Create" />
                    </p>
                 </fieldset>
        }

家庭控制器.cs

    public ActionResult Index()
    {
        ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

        var model = new OuterModel()
        {
            firstInnerModel = new FirstInnerModel(),
            secondInnerModel = new SecondInnerModel()
        };

        return View(model);
    }

    [HttpPost]
    public void ActivateFirst(FirstInnerModel ggg)
    {


    }

ggg.Title 返回空...

任何人?帮助!

4

1 回答 1

3

当您提交表单时,它会将 OuterModel 发布到控制器,因此您需要执行以下操作:

   [HttpPost]
    public void ActivateFirst(OuterModel ggg)
    {
        var whatever = ggg.FirstInnerModel.Title;

    }
于 2013-06-27T14:50:47.137 回答