Html.BeginForm 发布的模型为空

Html.BeginForm posted Model is null

您好,我遇到以下问题: 我试图通过表单 post 一个模型到另一个 controller-action。 但是收到的模型只填充了 null 个元素。

ToolController.cs

public class ToolController : Controller
{    
    public ActionResult Index()
    {
        var Model = new EditToolModel{ /* Some data */ };
        return View(Model);
    }

    [HttpPost]
    public ActionResult EditToolOverview(EditToolModel myModel)
    {
        return RedirectToAction("Index", "Tool", new { show = "overview" });
    }
}

EditToolModel.cs

public class EditToolModel
{
    public Tools tool;
    public IEnumerable<Tools> tools { get; set; }

    public ToolsExtention tool_extention;
    public string latest_version { get; set; }
    public string latest_version_type { get; set; }
    public string latest_devStep { get; set; }

    public IEnumerable<ToolVersionsView> versions { get; set; }
    public IEnumerable<DevelopmentStep> developmentSteps { get; set; }
}

Index.cshtml

@model EditToolModel
@{
    ViewBag.Title = "Index";
    Layout = "~/Layout/_Layout.cshtml";
}
@Html.Partial("ToolOverview", this.Model)

ToolOverview.cshtml

@model EditToolModel
@using (Html.BeginForm("EditToolOverview", "Tool", FormMethod.Post))
{
    <div class="contend">
        @Html.TextBoxFor(Model => Model.tool_extention.a)
        @Html.TextBoxFor(Model => Model.tool_extention.b)
        <input type="submit" name="tool_submit" value="Submit"/>
    </div>
}

ToolsExtention 尝试以 post 方法的形式将 EditToolOverview 属性 从 EditToolModel 更改为 ToolsExtention。

[HttpPost]
public ActionResult EditToolOverview(ToolsExtention myModel)
{
    //_devProv.changeToolExtention(myModel);
    return RedirectToAction("Index", "Tool", new { show = "overview" });
}

您需要在 tool_extention 属性 上安装 getter/setter 才能使 DefaultModelBinder 正常工作

public ToolsExtention tool_extention { get; set; }

tool 属性 也是如此(但在您看来,tool_extention 属性 的唯一呈现控件)