ASP.NET MVC动态属性和EditorForLabelFor如何实现?
- 内容介绍
- 文章标签
- 相关推荐
本文共计224个文字,预计阅读时间需要1分钟。

我目前正在使用MVC模式、3层架构以及Razor模板引擎。尝试结合新的动态ViewBag属性。想将ViewBag属性与EditorFor和LabelFor的Html助手类一起使用,但遇到了语法难题。View确实有@model设置,但我尝试使用的对象并非该模型的一部分。
我正在使用MVC 3 w / Razor并使用新的动态ViewBag属性.我想将ViewBag属性与EditorFor / LabelFor Html助手一起使用,但无法弄清楚语法.View确实有@model设置,但我尝试使用的对象不是该模型的一部分.我知道我可以创建一个ViewModel,但这不是我想要的.
有人可以帮忙吗?
控制器:
var myModel= _repo.GetModel(id); var newComment = new Comment(); ViewBag.NewComment = newComment; return View(myModel);
视图:
@model Models.MyModel @(Html.EditorFor(ViewBag.NewComment.Comment)) 我没有尝试过,但我认为这应该有效.
@(EditorFor(m => ViewBag.NewComment)
可以使用Linq-to-SQL语法,但在右侧使用完全不同的对象.
本文共计224个文字,预计阅读时间需要1分钟。

我目前正在使用MVC模式、3层架构以及Razor模板引擎。尝试结合新的动态ViewBag属性。想将ViewBag属性与EditorFor和LabelFor的Html助手类一起使用,但遇到了语法难题。View确实有@model设置,但我尝试使用的对象并非该模型的一部分。
我正在使用MVC 3 w / Razor并使用新的动态ViewBag属性.我想将ViewBag属性与EditorFor / LabelFor Html助手一起使用,但无法弄清楚语法.View确实有@model设置,但我尝试使用的对象不是该模型的一部分.我知道我可以创建一个ViewModel,但这不是我想要的.
有人可以帮忙吗?
控制器:
var myModel= _repo.GetModel(id); var newComment = new Comment(); ViewBag.NewComment = newComment; return View(myModel);
视图:
@model Models.MyModel @(Html.EditorFor(ViewBag.NewComment.Comment)) 我没有尝试过,但我认为这应该有效.
@(EditorFor(m => ViewBag.NewComment)
可以使用Linq-to-SQL语法,但在右侧使用完全不同的对象.

