S-Vuk
S-Vuk

Reputation: 369

TryUpdateModelAsync() can't pass in object parameters to update

I am having the strangest issue. I am trying to update a model's properties, like so:

        Answer = await dbContext.Answer.SingleOrDefaultAsync(p => p.ID == postedAnswer.AnswerID);

        // Set timestamp and answer
        Answer.AnswerTimestamp = DateTime.Now;
        Answer.SelectedAnswer = postedAnswer.SelectedAnswer;

        //Save changes
        await TryUpdateModelAsync(
            Answer,
            "Answer",
            p => p.AnswerTimeStamp, p.SelectedAnswer);

        // Save changes
        await dbContext.SaveChangesAsync();

However, intellisense is throwing an error on p.AnswerTimeStamp: "Answer does not contain a definition for AnswerTimeStamp and no extension method bla bla bla..."

One of the versions of TryUpdateModelAsync allows for passing in the model, a string name, and the parameters to update. In fact, I use the exact, VERBATIM, code in another Page, except with a different model with the same properties and it works there.

What is going on? This refuses to build

See this screenshot:

Problem

Intellisense recognizes that "p" is of type Answer, but no properties will show up when I begin typing them.

Upvotes: 2

Views: 1456

Answers (1)

sramekpete
sramekpete

Reputation: 3208

It is because IntelliSense is offering you different overload. TryUpdateModelAsync<TModel>(TModel, String, Func<ModelMetadata,Boolean>) instead of TryUpdateModelAsync<TModel>(TModel, String, Expression<Func<TModel,Object>>[]). You may want to explicitly say you want to use the one with includeExpressions parameter.

await TryUpdateModelAsync(
            Answer,
            "Answer",
            includeExpressions: p => new [] { p.AnswerTimeStamp, p.SelectedAnswer });

Related links:

TryUpdateModelAsync[TModel](TModel, String, Func[ModelMetadata, Boolean])

TryUpdateModelAsync[TModel](TModel, String, Expression[Func[TModel,Object]])

Upvotes: 2

Related Questions