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 cannot understand the syntax.
There is an @model set in the view, but the object I'm trying to use is not part of this model. I know I can create a ViewModel, but that is not what I want.
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))
I have not tried, but this should work, I think.
@(EditorFor(m => ViewBag.NewComment)
You can use Linq-to-SQL syntax, but use a completely different object on the right side.
, , :
@Html.EditorFor(ViewBag.NewComment)
, ViewBag , NewComment, , .
@Html.EditorFor(ViewBag.NewComment as Comment)
Update
, EditorFor . , EditorFor EditorForModel , ViewModel. , , ViewBag?
- ViewData , , Html.DisplayFor().
views ViewData
var newCommentModel = ( NewComment )ViewBag.NewComment;
,
@Html.DisplayFor( model => newCommentModel )
The expression tree now contains a strongly typed model, and the DisplayTemplate is displayed correctly.
Source: https://habr.com/ru/post/1781318/More articles:How to properly override BasicDataSource for Spring and Hibernate - javaCompiling with icc-static problem - c ++Adding a Method to the Rails ActiveRecord Class - ruby | fooobar.comHow to override save () method of modelform class and add missing information? - pythonHow to set local username in TortoiseSVN? - svnJava-based workflow solutions - javaRails 3 rescue_from, with and using custom modules - ruby-on-rails-3CQRS - Reading a DTO Confusion Model - .netLiquid Floating Elements Packed In A Container - htmlHAML и итерированный XML - rubyAll Articles