1

I've been looking for this conundrum for days now and been on many possible solutions, but either the examples are too far removed from what I am attempting and I am just not good enough to take in the data because of it. Hence I decided to ask the question myself and show my particular situation.

I got a product model like this (simplified)

public class Product
    {
        [Key]
        public int ProductId { get; set; }
        public string SKU { get; set; }
        public int ProductCategoriesId { get; set; }
        public bool Purchase { get; set; } 
        public bool ShowProduct { get; set; } 
        public virtual IList<ProductTranslations> ProductTranslations { get; set; }
    }

public class ProductTranslations 
    {
        [Key]
        public int ProductTranslationsId { get; set; }
        public int ProductId { get; set; }
        public string ProductLanguage { get; set; }
        public string Description { get; set; }
        public string ProductName { get; set; }
        public virtual Product Product { get; set; }
    }

So I have one product with language neutral data, then each product can have several child elements, one for each language. I have an edit form where I want to show the language neutral data and the language specific data (names and descriptions).

Simplified view:

@model Artikelhantering.Models.Product

@using (Html.BeginForm())
{
@Html.LabelFor(model => model.SKU)

@foreach (var item in Model.ProductTranslations)
{
    @Html.EditorFor(TranslationItem => item.ProductName) 
    @Html.TextAreaFor(TranslationItem => item.Description)
}

<p>
<input type="submit" value="Save" />
</p>
}

This gets everything onto the view nice and tidy looking I think, I think I could (should?) make a dedicated viewmodel for the editor-view later on. Anyway the problem is I don't know how to update the data on submit. I can update the language neutral data using normal EF approach but any changes to the fields from the other model are unchanged...

And to be honest that makes perfect sense to me, since there's nothing in the form to distinquish them, no unique id's or such and the product model does not contain those fields either... The way I would this in old classic asp would be to name the fields with their unique Id's and then make an SQL update query that updated each child model on submitting the form. I guess I could implement such a practice here too but it doesn't feel like the right way to go about things.

Is there a proper or 'best practices' approach in MVC for this kind of thing?

4

0 回答 0