2015-01-13 99 views
1

您好,我有以下問題: 我試圖通過表單發佈一個模型到其他控制器的行動。 但收到的模型只填充了「null」元素。 enter image description hereHtml.BeginForm發佈模型爲空

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

不是'@ Html.TextBoxFor(Model => Model.tool_extention.a)'但是@@ Html.TextBoxFor(m => m.tool_extention.a)' – demo

回答

4

你需要對tool_extention屬性爲getter/setter方法,以便使DefaultModelBinder工作

public ToolsExtention tool_extention { get; set; } 

同上爲tool屬性(但你只呈現控件在tool_extention財產您查看)

+0

謝謝,非常感謝。它解決了我的問題。 認爲'tool_extention'中的變量有getter和setter就足夠了。 –

+2

我假設是基於視圖,但是如果你只有'ToolsExtention'的渲染控件,爲什麼不只是讓你的模型成爲'ToolsExtention'(並且發回它而不是製作模型'EditToolModel'? –

+0

我有更多的東西要渲染,但我不想僅發佈整個代碼問題 –

-1

ToolsExtention嘗試改變從EditToolModel到ToolsExtention的EditToolOverview財產形式POST方法。

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