<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'保存表单'方法中公共ActionResult保存(TemplateViewModel模型)
我的模型包含来自表单的所有数据,但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 :(得分: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 {})
请原谅我,如果这次失败(再次),我是从臀部开枪。