所有模型属性的隐藏字段

时间:2013-12-11 13:43:13

标签: c# asp.net-mvc

为所有模型公共属性创建隐藏字段的最佳方法是什么?

我寻找类似的东西:

@Html.HiddenFor(t => t)

2 个答案:

答案 0 :(得分:2)

我想你想要这样做的原因是在编辑屏幕中更新实体记录时获取其他属性值。您可能正在编辑一些属性(并且只有您的表单中的那些属性),并且您可能会获得null所有其他不在表单中的属性。

您应该做的是,只将PostID属性保留在表单的隐藏字段中,并在HttpPost操作方法中,读取实体并仅更新从表单发送的属性并保存

[HttpPost]
public ActionResult Edit(Post model)
{
  var existingPost=repositary.GetPost(model.PostID);

  //Set only the properties posted from form to the existingPost entity
  existingPost.Title=model.Title;

  var result= repositary.SavePost(existingPost);
  return RedirectToAction("PostSaved",new {@id=model.PostID});
}

答案 1 :(得分:0)

您可以使用http://jqueryui.com/dialog/来显示确认窗口。在Ok按钮上单击指定表单提交。 见下面的例子:

@using(Ajax.BeginForm("Edit", "Post", null, new AjaxOptions { HttpMethod = "POST" }, new {@id = "frmPost" , enctype = "multipart/form-data" }))
{ 
    @Html.EditorForModel()
}

<div id="dialog">Some confirmation</div>        

<script>
    $("#dialog").dialog({
        modal: true,
        buttons: {
            Ok: function () {
                $("#frmPost").submit();
                $("#dialog").dialog('close');
            },
            Cancel: function () {
                $(this).close();
            }
        }
    });
</script>
相关问题