开发者

ASP.NET MVC ViewModel and Request Variables

Greetings!

I need assistance with ASP.NET MVC and the values posted from a form operation (form is inside a Partial View).

On my page to create a User Account, v开发者_StackOverflow社区arious form fields collect the information. The Partial View is backed by a ViewModel (UserAccountViewModel).

To validate and/or save the information, I need to pass the values of the form to UserAccountService. Back in Java and Struts 1.x, I used the getMap() method of the DynaActionForm, but being an ASP.NET newbie, I'm not sure of the best way to proceed.

On a post operation, are the fields of the ViewModel automatically updated? If that's the case, I could pass the ViewModel to the Service layer (not my preferred solution, but good enough).

Jason


You can use the UpdateForm method in .net to have it automatically map the form data to your model based on similar naming.

UserAccountViewModel.UpdateForm(Request.Form);

UpdateForm will work on pretty much any class with properties, you just need to import the namespace. Here is a pretty good tutorial on form handling in asp.net mvc that uses this method. This sounds like it's a similar method to what you're used to in Java.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜