gpt4 book ai didi

asp.net-mvc - ASP.Net MVC、动态属性和 EditorFor/LabelFor

转载 作者:行者123 更新时间:2023-12-01 10:11:45 25 4
gpt4 key购买 nike

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

View 确实有一个@model 集,但我尝试使用的对象不是该模型的一部分。我知道我可以创建一个 ViewModel,但这不是我所追求的。

有人能帮忙吗?

Controller :

   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 语法,但在右侧使用完全不同的对象。

关于asp.net-mvc - ASP.Net MVC、动态属性和 EditorFor/LabelFor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4493877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com