Maximilian Ast Maximilian Ast - 3 months ago 34
ASP.NET (C#) Question

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
{
private IDevelopmentStepProvider _devProv;

public ToolController(IDevelopmentStepProvider devtool, IToolProvider tool, IDevelopmentStepVersionsProvider versProv)
{
_devProv = devtool;
}

public ActionResult Index()
{
var Model = new EditToolModel();

/*
* assigning some data to Model
*/

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)
@Html.TextBoxFor(Model => Model.tool_extention.c)
@Html.TextBoxFor(Model => Model.tool_extention.d)
<input type="submit" id="tool_submit" name="tool_submit" value="Submit" class="btn submit"/>
</div>
}

Answer

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)

Comments