Html.BeginForm发布Model为null

时间:2015-01-13 12:33:14

标签: c# asp.net asp.net-mvc-4 razor html-helper

您好我有以下问题: 我试图通过表单将模型发布到其他控制器操作。 但是收到的模型只填充了“null”元素。 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>
}

2 个答案:

答案 0 :(得分:5)

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

public ToolsExtention tool_extention { get; set; }

同样适用于tool属性(但您对视图中tool_extention属性的唯一渲染控件)

答案 1 :(得分:-1)

ToolsExtention尝试使用表单post方法将EditToolModel中的EditToolOverview属性更改为ToolsExtention。

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