3

I am using MVC 3 w/ Razor and using the new dynamic ViewBag property. I would like to use the ViewBag property with the EditorFor/LabelFor Html helpers but can't figure out the syntax.

The View does have a @model set, but the object I am trying to use is not part of that model. I am aware I can create a ViewModel but that is not what I am after.

Can anyone help?

Controller:

   var myModel= _repo.GetModel(id);
   var newComment = new Comment();

   ViewBag.NewComment = newComment;

   return View(myModel);

View:

@model Models.MyModel

@(Html.EditorFor(ViewBag.NewComment.Comment))

3 Answers 3

5

I haven't tried it, but this should work I think.

@(EditorFor(m => ViewBag.NewComment)

It is possible to use a Linq-to-SQL syntax, but use a completely different object on the right side.

Sign up to request clarification or add additional context in comments.

2 Comments

@bahadirarslan This doesn't work (An expression tree cannot contain a dynamic expression). As a possible workaround you can cast the dynamic model to its underlying type (See my answer for this workaround).
This also doesn't work for custom editors. For instance using a UIHint like this [DataType(DataType.Html), UIHint("tinymce_jquery_full"), AllowHtml] on your model. It's better to create a partial view and pass a model object to it.
1

Not knowing what your Comment Model looks like, my gut reaction would be to just do:

@Html.EditorFor(ViewBag.NewComment)

However, because ViewBag is dynamic, you may need to cast NewComment before you use it, in order to get the EditorFor magic.

@Html.EditorFor(ViewBag.NewComment as Comment)

Update

Strike that, EditorFor can only accept an Expression as a parameter, and that Expression must return a property of the page model. I don't think EditorFor or EditorForModel are going to be of any use to you if you don't want to use a ViewModel. Have you considered switching the roles of whatever it is you're using the Model for, with that of the ViewBag?

3 Comments

did you mean "Have you considered switching the roles of whatever it is you're using the Model for, with that of the VIEWMODEL" ? Otherwise, I am not sure what you mean.
Hi, I mean EditorFor seems to be geared for use with the page's model. It is certainly the way I've always used it. But I can see merit in what you're trying to achieve. What I mean is, you're currently assigning a model from your repository to the page model. What if you assigned the repo model to the ViewBag and the Comment to the page model? Is that possible in your View? Could you elaborate on why you don't want to use a ViewModel?
I have several models I am using on the page, so I can't just swap one for another. What is frustrating is I think it is possible (or should be), just don't know the syntax.
1

If for some reason I need to use ViewData to pass the model into my view I do the following to allow for the Html.DisplayFor() helpers.

In the views code block I cast the ViewData model object to its underlying type

var newCommentModel = ( NewComment )ViewBag.NewComment;

Then I assign the following expression to the helper using the strong-typed reference

@Html.DisplayFor( model => newCommentModel )

The expression tree now contains a strongly-typed model and the DisplayTemplate is correctly displayed.

Comments

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.