我想做的事:
我想以这种方式在MVC应用程序上运行方法:
public static void test()
{
ModelVariables hey = new ModelVariables();
hey.test = "hello world";
}
在这里,我基本上是将“ hello world”存储到全局变量中,简单吗?
public class ModelVariables
{
public string test { get; set; }
}
现在我的公共字符串“ test”具有字符串“ hello world”,到目前为止还不错吧?
然后我想通过以我的html形式打印来验证此方法是否有效:
@{
ModelVariables testee = new ModelVariables();
ViewBag.Title = "Index";
}
@using (Html.BeginForm())
{
<div>@testee.test</div>
}
@ testee.test =“ hello world”,或者看起来...当我运行该应用程序时,什么都没有显示!wtf正在进行,我该如何解决?
如果要将值传递给MVC中的视图,则应将其放在ViewModel中。
如果将此值放在ViewModel中,则流程应如下所示:
ViewModel /模型:
public class TestViewModel
{
public string testee { get; set; }
}
看法:
@model testproject.Models.ViewModels.TestViewModel
@{
ViewBag.Title = "Index";
}
@using (Html.BeginForm())
{
<div>@Html.DisplayFor(m=>m.testee)</div>
}
FooController:
public ActionResult Index()
{
var model = new TestViewModel();
model.testee = "hello world!";
return View(model);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句