Search code examples
c#asp.netasp.net-mvc-4razorhtml-helper

Html.BeginForm posted Model is null


Hello I have following problem: I tried to post a Model through a Form to an other controller-action. But the received Model is only filled with null elements. enter image description here

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

Solution

  • You need to have a getter/setter on the tool_extention property in order for the DefaultModelBinder to work

    public ToolsExtention tool_extention { get; set; }
    

    Ditto for the tool property (but your only rendering controls for the tool_extention property in your view)