I have a simple object
public class SomeObject
{
public Int32 id { get; set; }
public string name { get; set; }
}
In a strongly typed view I am letting the user edit SomeObject.name, when the form is posted the receiving method doesn't see SomeObject.id in FormCollection (it does see SomeObject.name). Do I need to actually place every object proper开发者_如何学JAVAty in the form to be able to access them when form is posted?
What's the best practice, should I just insert hidden fields for each property I don't plan letting the user edit? maybe I should place the entire object in the ViewData?
Thanks
FormCollection
contains only properties that have been posted either through text fields or hidden fields. So if you need to use the Id
property in your controller action you need to include it in your form. Depending on what you are doing in your controller action you might or might not include it. It is not necessary to include hidden fields for each property.
Usually the Id
is sufficient because it allows you to later retrieve the object from your data store given this id.
What does your action method look like to which you are posting the form? If your method to handle the GET requests takes id, if your POST method also takes id and you are using the BeginForm helper method with none of the parameters overloaded, the form method will take the id as a parameter and you won't need to worry about including hidden fields for the id itself.
精彩评论