2013-04-10 88 views
1

我有 ASP.NET MVC窗體彈出窗口中有一些控件和部分(數據網格)與他自己的模型。 這裏彈出:如何從窗體中獲取部分數據

<div id="AddEditDialog" class="none"> 
    @using (Ajax.BeginForm("Save", "Templates", new AjaxOptions 
     { 
      InsertionMode = InsertionMode.Replace, 
      UpdateTargetId = "AddEditPlaceHolder", 
      OnSuccess = "OnSaveSuccess", 
      HttpMethod = "Post" 
     })) 
    { 
    <div> 
     <div id="AddEditPlaceHolder"></div> 
     <div id="PopupButtons" class="btn-holder-centered"> 
      <input type="submit" value="Save" name="SaveButton" /> 
      <input type="button" value="Cancel" name="SaveCancelButton" id="CancelEditHandler" /> 
     </div> 
    </div> 
    } 
</div> 

這裏是形式,我在AddEditPlaceHolder通過JS渲染:

@model TemplatesViewModel 
<div class="form-field-plain overflow"> 
    <div class="forRow narRow float-left"> 
     @Html.LabelFor(x => x.Revocable) 
     @Html.CheckBoxFor(x => x.Revocable) 
    </div> 
</div> 
<div class="form-field-plain overflow"> 
    <div class="forRow narRow float-left"> 
     @Html.LabelFor(x => x.HtmlTemplate) 
     @Html.TextAreaFor(x => x.HtmlTemplate) 
    </div> 
</div> 

@Html.Partial("_VariablesGridView", Model.Variables) 

_VariablesGridView.cshtml:

@model List<TemplateVariableViewModel> 

<table id="TemplateVariablesGrid"> 
    <thead> 
     <tr> 
      <td>Tag/Code</td> 
      <td>Prompt</td> 
      <td>Action</td> 
     </tr> 
    </thead> 
    <tbody> 
     @foreach (var i in Model) 
     { 
      <tr> 
       <td> 
        @Html.TextBox("txtTag", @i.Tag, new {}) 
       </td> 
       <td> 
        @Html.TextBox("txtPrompt", @i.Prompt, new { }) 
       </td> 
       <td> 
        @Html.HiddenFor(x => x.First(s => s.Id == @i.Id).Id) 
        <label class="delete-variable">delete</label> 
       </td> 
      </tr> 
     } 
    </tbody> 
</table> 
<br /> 
<input type="button" name="btnAddTemplateVariable" value="add new variable"/> 
<br /> 

我的問題是:在 控制器「保存窗體'方法public ActionResult保存(TemplateViewModel模型)
我的模型包含所有數據f ROM形式,但TemplateViewModel.Variables是空的

有沒有什麼方法可以填充它呢?

型號:

public class TemplateViewModel 
{ 
    public int Id { get; set; } 
    public string HtmlTemplate { get; set; }  
    public List<TemplateVariableViewModel> Variables { get; set; } 
} 

public class TemplateVariableViewModel 
{ 
    public int Id { get; set; } 
    public string Tag { get; set; } 
    public string Prompt { get; set; } 
} 
+0

您需要發佈從_ConsentVariablesGridView.cshtml – 2013-04-10 14:42:22

+0

標記,我只是將它加入 – 2013-04-10 14:48:00

回答

0

我相信這是因爲ASP.Net MVC結合不投入方面,這些領域,看看投放到瀏覽器的字段名稱,什麼是txtTag由前綴時它到達瀏覽器,什麼是你是後執行下列操作:

@Html.Partial("_VariablesGridView", Model) 

_VariablesGridView.cshtml:

@model TemplatesViewModel 
... 
@for (int i = 0; i < Model.Variables.Count; i++) 
    @Html.TextBox("txtTag", @Model.Variables[i].Tag, new {}) 

原諒我,如果這失敗(再次),我從臀部射擊。

+0

它看起來懂事,讓我來試試吧 – 2013-04-10 15:06:51

+0

不幸的是 - 它不是一個解決方案。我在「視圖」中修復了字段命名,但在提交時TemplateViewModel.Variables仍爲空 – 2013-04-10 15:20:25

+0

嘗試更新後的for循環。在此之後,我會接受我的悲慘失敗:) – 2013-04-10 15:22:49