PDA

View Full Version : [Razor] Populating form values from controller



machinableed
May 06, 2012, 10:33 AM
Hi there,

I'm just having some issues populating a form with data that I must retrieve from the controller. There are many examples in webforms but I'm just wondering what the best way to do this in Razor views would be. Basically I have a details view which receives an ID parameter into its controller method and retrieves the necessary data but now I'm not sure what to do with it. Must I create a store for the form and some sort of listener or can I simply set up the relevant model object for my form view (as you do in normal MVC) and then just set the text field values (for instance) as Model.Fieldname etc.? It seems like there are several ways to achieve the same thing and I'm not sure about the advantages/disadvantages of the various approaches?

Timothy
May 06, 2012, 7:06 PM
What about just passing your data as a model to your view and binding your controls from your Model? You don't need to make your controllers and views complicated, remember to KISS ... that's the whole idea of MVC :)



@model MyNamespace.Models

@(Html.X().FormPanel()
....
.Items(a =>
{
a.Add(Html.X().TextField()
.Name("FirstName")
.Text(Model.FirstName)
} )
)


Cheers,
Timothy

Daniil
May 07, 2012, 10:42 AM
Hi,

+1 to Timothy.

The following links might be helpful as well.
http://forums.ext.net/showthread.php?18729&p=80909&viewfull=1#post80909
http://msdn.microsoft.com/en-us/gg618479