user1464139
user1464139

Reputation:

Problems updating my data with MVC?

I have the following class:

public class Content
{
    public string PartitionKey { get; set; }
    public string RowKey { get; set; }
    [Required(ErrorMessage = "Title required")]
    [DisplayName("Title")]
    public string Title { get; set; }
    [DisplayName("Status")]
    public string Status { get; set; }
    public string Type { get; set; }
    public string ContentType
    {
        get { return PartitionKey.Substring(2, 2); }
    }
}

I added the ContentType as it's something I need to be able to read (not set). However when I try to save a record I get the following message:

The closed type System.String does not have a corresponding ContentType settable property.

Did I make a mistake with the way I added the ContentType property? What I am wondering is if I should use a viewModel. But how can I do that? Do I just need to copy each of the properties from my class and add a get and set for each? Plus then just add ContentType.
From what I understand there is a way where I just add the class Content to the view model but then I think I would have to change all the Model.Status in my views to Model.Content.Status. I'd rather just still have Model.Status in my views.

What about my checks and display properties. Do I need to replicate them in the viewModel or they just pass through?

Upvotes: 1

Views: 127

Answers (1)

linkerro
linkerro

Reputation: 5458

The model binder needs read-write properties to be able to correctly set the data in your model class.

It uses reflection to set values to properties for a new instance of the type that you declared as your model.

Upvotes: 1

Related Questions