ASP.Net MVC,动态属性和EditorFor / LabelFor(ASP.Net MVC, Dynamic Property and EditorFor/LabelFor)

我正在使用MVC 3 w / Razor并使用新的动态ViewBag属性。 我想用EditorFor / LabelFor Html助手使用ViewBag属性,但无法弄清楚语法。

该视图确实有@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))

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))

最满意答案

我没有尝试过,但我认为这应该有效。

@(EditorFor(m => ViewBag.NewComment)

可以使用Linq-to-SQL语法,但在右侧使用完全不同的对象。

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.

更多推荐