【发布时间】:2013-05-26 23:52:27
【问题描述】:
我认为这应该是一个更容易的任务:
编辑:
直到今天,Asp.Net MVC 似乎都无法为这种情况提供一个简洁的解决方案:
如果你想将一个简单的字符串作为模型传递,并且你不必定义更多的类和东西来这样做...... 有什么想法吗??
我在这里尝试一个简单的字符串模型。
我收到此错误:
"Value cannot be null or empty" / "Parameter name: name"
观点:
@model string
@using (Html.BeginForm())
{
<span>Please Enter the code</span>
@Html.TextBoxFor(m => m) // Error Happens here
<button id="btnSubmit" title="Submit"></button>
}
控制器:
public string CodeText { get; set; }
public HomeController()
{
CodeText = "Please Enter MHM";
}
[HttpGet]
public ActionResult Index()
{
return View("Index", null, CodeText);
}
[HttpPost]
public ActionResult Index(string code)
{
bool result = false;
if (code == "MHM")
result = true;
return View();
}
【问题讨论】:
-
您是否尝试过在返回索引操作中的视图之前初始化 CodeText?
CodeText = ""; return View(Codetext); -
@dlebech 我在构造函数中做到了
-
您是否已经尝试过
@Html.TextBox("code", Model)?我实际上不确定您是否需要为模型绑定指定一个名称才能在帖子中工作。 -
@roliu 结果:“未找到视图'索引'或其主人”