Asp.net MVC UpdateModel(myClass) 的基本问题
在 Asp.net MVC 1 应用程序的控制器中,我想使用 UpdateModel 用控制器中的 POST 数据填充变量。我已经看过几十个例子,但即使是最基本的例子对我来说似乎也默默地失败了。
这是一个非常基本的示例,但不起作用。 我做错了什么?
public class TestInfo
{
public string username;
public string email;
}
public class AdminController : Controller
{
public ActionResult TestSubmit()
{
var test = new TestInfo();
UpdateModel(test);//all the properties are still null after this executes
//TryUpdateModel(test); //this returns true but fields / properties all null
return Json(test);
}
}
//Form Code that generates the POST data
<form action="/Admin/TestSubmit" method="post">
<div>
<fieldset>
<legend>Account Information</legend>
<p>
<label for="username">Username:</label>
<input id="username" name="username" type="text" value="" />
</p>
<p>
<label for="email">Email:</label>
<input id="email" name="email" type="text" value="" />
</p>
<p>
<input type="submit" value="Login" />
</p>
</fieldset>
</div>
</form>
In my Controller in a Asp.net MVC 1 app I want to use UpdateModel to populate a variable with POST data in my controller. I've looked at dozens of examples but even the most basic ones seem to fail silently for me.
Here's a very basic example that's just not working.
What am I doing wrong?
public class TestInfo
{
public string username;
public string email;
}
public class AdminController : Controller
{
public ActionResult TestSubmit()
{
var test = new TestInfo();
UpdateModel(test);//all the properties are still null after this executes
//TryUpdateModel(test); //this returns true but fields / properties all null
return Json(test);
}
}
//Form Code that generates the POST data
<form action="/Admin/TestSubmit" method="post">
<div>
<fieldset>
<legend>Account Information</legend>
<p>
<label for="username">Username:</label>
<input id="username" name="username" type="text" value="" />
</p>
<p>
<label for="email">Email:</label>
<input id="email" name="email" type="text" value="" />
</p>
<p>
<input type="submit" value="Login" />
</p>
</fieldset>
</div>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来您正在尝试让控制器根据表单元素更新模型。请尝试这样做:
在您的代码中,您正在创建一个新的 TestModel,而不是让 MVC 运行时从 HttpPost 序列化它。我也让自己在这件事上陷入困境,你不是唯一的一个!
It looks like you're trying to get the controller to update the model based on the form elements. Try this instead:
In your code, you're creating a new TestModel instead of letting the MVC runtime serialize it from the HttpPost. I've let myself get wrapped around the axel on this also, you're not the only one!
使您的公共领域具有以下属性:
make properties of your public field:
我对 ASP.NET MVC 不太熟悉,但您的 TestSubmit 方法不应该看起来更像这样:
I'm not too familiar with ASP.NET MVC, but shouldn't your TestSubmit method look more like this:
在控制器中,您应该有两种方法,一种用于响应 GET,另一种(如果需要)用于响应 POST。
因此,首先有一个 GET 方法:
其次,您需要一个 POST 方法:
请注意,那里没有
UpdateMethod
,即 ModelBinder 会为您完成此操作。In the controller you should have two methods, one to respond to the GET, the other, if required is for responding to the POST.
So, firstly have a GET method:
Secondly, you'll need a POST method:
Notice that there's no
UpdateMethod
there, the ModelBinder would have done that for you.