我有以下视图,主要查看项目列表作为显示: -
@foreach (var item in Model.Resources) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.SystemInfo.MODEL)
</td>
<td>
@Html.DisplayFor(modelItem => item.RESOURCENAME)
</td>
<td>
@Html.DisplayFor(modelItem => item.ResourceLocation.SiteDefinition.AccountDefinition.SDOrganization.NAME)
</td>
<td>
@Html.DisplayFor(modelItem => item.ResourceLocation.SiteDefinition.SDOrganization.NAME)
</td>
<td>
@Html.DisplayFor(modelItem => item.ComponentDefinition.COMPONENTNAME)
</td>
<td>
@Html.DisplayFor(modelItem => item.ResourceState.STATEDESC)
</td>
<td id = "@item.RESOURCEID">
@using (Ajax.BeginForm("CreateOn","VirtualMachine", new AjaxOptions {
InsertionMode = InsertionMode.Replace,
UpdateTargetId = item.RESOURCEID.ToString() ,
LoadingElementId = "progress",
HttpMethod = "POST"})){
<span class="f">Hypervisor Server</span>
@Html.DropDownListFor(model =>model.VirtualMachine.ServerID, ((IEnumerable<t.Models.Server>)ViewBag.Servers).Select(option => new SelectListItem {
Text = (option == null ? "None" : option.Technology.Tag),
Value = option.ServerID.ToString(),
Selected = (Model != null) && (Model.VirtualMachine != null) && (option.ServerID == Model.VirtualMachine.ServerID)
}), "Choose...")
@Html.ValidationMessageFor(model =>model.VirtualMachine.ServerID)
@Html.Hidden("IT360id", item.RESOURCEID)
@Html.Hidden("CustomerName",item.ResourceLocation.SiteDefinition.AccountDefinition.SDOrganization.NAME)
@Html.Hidden("SiteName",item.ResourceLocation.SiteDefinition.SDOrganization.NAME)
@Html.Hidden("ResourceName",item.RESOURCENAME)
<input type="submit" value="Add To" class="btn btn-primary"/>
}
但是用户可以使用ajax.beginform读取数据并选择在我们的数据库上创建项目。但要这样做,我需要将DisplayFor的一些值传递给模型绑定器。目前我已将所有需要的数据添加为隐藏字段,然后我将这些值传递给我的操作方法,如下所示: -
[HttpPost]
public ActionResult CreateOn(VirtualMachineOnIT360Only vm, long IT360id, string CustomerName, string SiteName, string ResourceName)
{
我发现我的方法不太可靠,我试图找出一个更可靠的解决方案。那么有人可以提出建议吗? 感谢
答案 0 :(得分:1)
要发布DisplayFor,我更喜欢使用hiddenfor而不是像这样隐藏:
@Html.HiddenFor(item => item.RESOURCENAME);