MVC 4 / Entity Framework 5 / Code First - Persisting a collection (list of objects) in a view - a few questions

StackOverflow https://stackoverflow.com/questions/15337816

Вопрос

My project has several models, some with 1:many relationships with other models. A user has a profile page, this is split up into several partial views - each grouping/representing specific attributes of the given model.

If for example, I want to display 5 of the 10 properties of a given model in a partial view, and want to persist the properties that aren't shown to the user I'm using Html.HiddenFor like so

@Html.HiddenFor(x => x.IsInGroup)

This works for single entry properties as above. Of the 5 hidden member attributes, one of these may be a list. I understand that a list cannot be persisted using HiddenFor. I've come across the Mvc Futures package and the Html.Serialize helper extension, but I'm not totally sure how to use it and havn't been able to find much good information on it.

  • Once a model is passed to a view everything associated with it (properties, collections) is available for us to access e.g. Model.Username. If for example we don't use HiddenFor with Username, does that mean it will be reset to a default value, or null? Is this the same for collections?
  • Is using HiddenFor the only/best way to persist a model's properties that are not shown to the user?
  • Is using Html.Serialize the only way persist a model's collection that is not shown to the user? Other options?
  • Could someone provide/link to a simple example of how to persist a collection in a situation similar to what I've described?
  • If I was to use Html.Serialize, does the whole model need to be serialised or can I just serialise the collection?

Feedback appreciated.

Это было полезно?

Решение

You don't need to "persist" any data from your model by creating all the fields (hidden or not) from the model. In controller, when updating the record in the database you will just update what you need, the rest will not be affected.

Let's say this is your Product model

class Product { 
    public int ID {get; set;}
    public string Name {get; set;} 
    public string Description {get; set;}
    public decimal Price {get; set;}
    public virtual ICollection<Category> Categories { get; set; }
}

If you only want to edit Name and Description in your view, then you can only put those fields, and skip the rest (the ID would be in a hidden field). When the form is submitted, in your controller you would reference the Product record by the ID that got passed in and then you can update Name and Description to what you received from the form. Categories and the price will not be affected.

Somewhat more straight forward way to do this is to use a ViewModel specific to the view without non-editable properties. I found this post that gives more details how to do this:

Successful Model Editing without a bunch of hidden fields

On a smaller project you don't have use any kind of library (AutoMapper, InjectValues, ...), but you can do it yourself in places where you need to copy the values from a ViewModel into an actual record from DB.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top