0

What is the best way to create hidden fields for all model public properties?

I look for something like:

@Html.HiddenFor(t => t)
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
another_user
  • 3,590
  • 2
  • 12
  • 7

2 Answers2

3

I guess the reason you want to do this is to get the other property values when you update your entity record in an Edit screen. you may be editing a few properties (and only those are in your form) and you may be getting null for all other properties which was not in the form.

What you should be doing is,Keep only the PostID property in a hidden field in your form and In your HttpPost action method, Read the entity and update only those properties which was sent from the form and save it.

[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});
}
Shyju
  • 214,206
  • 104
  • 411
  • 497
0

You could use http://jqueryui.com/dialog/ for displaying confirmation window. Specify form submit on Ok button click. See example below:

@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>