【发布时间】:2013-02-01 22:21:44
【问题描述】:
我的问题是关于 MVC 中的绑定。你能帮我吗?
我的控制器:
public class TestController : Controller
{
//
// GET: /Test/
[HttpGet]
public ActionResult Index()
{
return View(new TestModel());
}
public ActionResult Index(TestModel test)
{
return View(test);
}
}
我的看法:
@model MvcApplication1.Models.TestModel
@using (Html.BeginForm())
{
@Html.TextBoxFor(x => x.test) // or x.Test
<input type="submit" value="Set"/>
}
我的模特:
public class TestModel
{
public string test { get; set; } // or Test{get;set;}
}
据我了解,问题与控制器中参数“test”的名称有关。我刚刚将其更改为“模型”并且绑定正在工作。但它在原始状态下不起作用(参数名称为'test'),'test'参数为空。
请让我理解为什么绑定在当前示例中不起作用。非常感谢!
【问题讨论】:
-
模型中测试参数的值为空,因为它从未被设置为任何值。
-
我不明白为什么。我的帖子正文有 test=value。首先,活页夹正在帖子正文(以及其他一些地方)中搜索“test.test”。之后它应该搜索简单的“测试”并绑定到模型。正确的 ?或者,我认为绑定过程是错误的..
标签: asp.net-mvc binding