use*_*331 5 c# asp.net asp.net-mvc
我有这样的控制器:
public class PreviewController : Controller
{
// GET: Preview
public ActionResult Index()
{
string name = Request.Form["name"];
string rendering = Request.Form["rendering"];
var information = new InformationClass();
information.name = name;
information.rendering = rendering;
return View(information);
}
}
Run Code Online (Sandbox Code Playgroud)
在视图中,我正在尝试信息.name如下:
@ViewBag.information.name
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
@information.name
Run Code Online (Sandbox Code Playgroud)
但两者都有相同的错误:
无法对空引用执行运行时绑定
我究竟做错了什么?
你必须@Model.name在视野中使用.没有@ViewBag.information.name.同样在视图的顶部,您必须定义如下内容:
@model Mynamespace.InformationClass
Run Code Online (Sandbox Code Playgroud)
使用MVC的模型绑定功能会更好.因此,请更改您的操作方法:
public class PreviewController : Controller
{
[HttpPost] // it seems you are using post method
public ActionResult Index(string name, string rendering)
{
var information = new InformationClass();
information.name = name;
information.rendering = rendering;
return View(information);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 4
在视图中只需输入
@Model.name
Run Code Online (Sandbox Code Playgroud)
由于 InformationClass 是您的模型,因此您只需使用@Model从视图中调用其属性
| 归档时间: |
|
| 查看次数: |
4072 次 |
| 最近记录: |