我在弹出窗口中有 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>
这是我通过 js 在 AddEditPlaceHolder 中呈现的表单:
@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 />
我的问题是:在 Controller 'save form' 方法 public ActionResult Save(TemplateViewModel model)
我的模型包含来自表单的所有数据,但 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; }
}