0

I have a complex model object that contains a number of child objects detailing the page status, data and configuration. The model's children are defined as base classes and are actually passed as instances of descendant classes and passed to the view to be rendered.

I have the model object being passed OK and I can create local variables that hold the data and config objects but now I want to display some values in text boxes and their labels.

Here is the variable creation in the view:

@model PageBase
@{
    PageConfigPersonal config = (PageConfigPersonal)Model.PageConfig;
    PageDataPersonal data = (PageDataPersonal)Model.PageData;
}

I can't figure out how, or if, Html.TextBoxFor can be used with these local variables - can this be done and if so how?

I know that I can cast the model but that is going to get really unwieldy and messy so would like to use the local variable instead.

Gordon

1 Answer 1

1

still you can use your local variable for TextBoxFor as below,

@Html.TextBoxFor(m => config.<model variable name>)
Sign up to request clarification or add additional context in comments.

2 Comments

I don't think this is correct as the Lambda expression must return a property inside the model, config is not
I can confirm that using m => data.Name results in a control with an id of "config.Name" which does not work well when posting back to the server. At the moment I am casting using ((PageDataPersonal)m.PageData).Name and this is working although it is a handful to type. I was just looking for a simpler way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.