Asp.net mvc ASP.NET MVC中的绑定
我的问题是关于MVC中的绑定。你能帮我吗 我的控制器:Asp.net mvc ASP.NET MVC中的绑定,asp.net-mvc,binding,Asp.net Mvc,Binding,我的问题是关于MVC中的绑定。你能帮我吗 我的控制器: public class TestController : Controller { // // GET: /Test/ [HttpGet] public ActionResult Index() { return View(new TestModel()); } public ActionResult
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"/>
}
据我所知,问题与控制器中参数“test”的名称有关。我刚刚把它改为“model”,绑定正在工作。但它在原始状态下不工作(参数名称为“test”),“test”参数为null
请告诉我为什么绑定在当前示例中不起作用。多谢各位 第二个方法需要一个
[HttpPost]
属性。您也不能使用test
作为变量名,因为它与您试图绑定到的类的属性同名;ModelBinder无法确定要使用哪个。您的代码如下所示:
public class TestController : Controller
{
//
// GET: /Test/
[HttpGet]
public ActionResult Index()
{
return View(new TestModel());
}
//
// POST: /Test/
[HttpPost]
public ActionResult Index(TestModel testModel)
{
return View(testModel);
}
}
它不起作用。作用中的测试变量“Index(TestModel test)”为空。默认绑定仍然不起作用。是否正在修改ModelBindingContext?不,这只是我添加到空MVC4新项目中的代码。我添加了这些控制器、视图和模型,没有其他内容。也许是默认绑定的具体实现,我不知道。哦!我刚刚意识到类中属性的名称与索引方法中变量的名称相同。看看这个问题的答案:是的,我理解。我在一些书中读过关于默认绑定的内容,但是我找不到关于它的注释。你能提供一些关于它的参考资料吗?正如我所知,第一个-绑定器在某些地方搜索test.test(在post、查询字符串、路由中的默认值等),第二个-绑定器应该只搜索-“test”并将其绑定到我的模型:(模型中test参数的值为null,因为它从未被设置为任何值。我不明白-为什么。我的帖子正文有test=value。首先,绑定器在帖子正文中搜索“test.test”(以及其他一些地方)。之后,它应该搜索简单的“test”,并绑定到模型。对吗?或者,我认为绑定过程是错误的。)。。
public class TestController : Controller
{
//
// GET: /Test/
[HttpGet]
public ActionResult Index()
{
return View(new TestModel());
}
//
// POST: /Test/
[HttpPost]
public ActionResult Index(TestModel testModel)
{
return View(testModel);
}
}