tags:

views:

1153

answers:

1

i create a strongly type LocationViewData has a properties

...
public Location location { get; set; }
...

which is Location model (location.address, location.country ..etc)

in controller

public ActionResult Edit(int id)
{
    ...
    LocationViewData ld = new LocationViewData();
    ...
    return View(ld);
}

in view codebehind

public partial class Edit : ViewPage<MvcTest.Models.LocationViewData>
{
}

And my question is how i can auto get value from location properties in viewdata to display on textbox (ViewData.Model.location.address).

<%=Html.TextBox("address") %>

i don't want specify each field like this

<%=Html.TextBox("address", ViewData.Model.location.address) %>
+1  A: 

It already works exactly like that. Start a new MVC (beta 1) project, and put this in the "Index" action of the "Home" controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
     this.ViewData.Model = new MyObject
     {
      Name = "Timmy",
      FavColor = "Blue",
     };

     return View();
    }

    public class MyObject
    {
     public string Name { get; set; }

     public string FavColor { get; set; }
    }
}

Now, put this in the View:

<%=Html.TextBox("FavColor") %>

It'll say "Blue" in it. It tries to bind by the name already (MVC checks a few places, one of which is the "Model").

EDIT: What you need to do is:

  1. Make sure that "location" is a property, and that "address" is a property.

  2. put "location.address" as the name... not just "address".

Timothy Khouri
about your 2th way, it will set name of text box is location.address, and when submit to controller, it can not map into "string address param" of controller method
complez