tags:

views:

71

answers:

1

First let me thank all of you for numerous useful posts I have read during my recent ASP.NET MVC development. My questions is as follows:

I have a ASP.NET MVC application with loosely coupled model, meaning I use a business layer assembly in a controller to access the data and return data to a view by setting Viewdata["MyData"] from the controller.

Now my question is how to set individual data of a user control which has multiple instances in a MVC page.

Eg. I have a text box inside userctl.ascx as:

<%= Html.TextBox("ApprovalDate", ViewData["ApprovalDate"],
                 new { @ReadOnly = "True" })%>

If i set ViewData["ApprovalDate"] to be something, how does it apply to individual control data?

Appreciate your help.

+1  A: 

In your case it would work if you set your user controls (partial views in MVC terminology) to be strongly typed.

Then you can initialize the model and pass it to each individual partial view without those values getting mixed up.

Of course, you need to pass to the view the data for all hosted controls in some form.

Developer Art
This was my first guess. Thanks.
RDongre
Also is there any way to name those user controls; my aim is to write some script validations for the contained controls.
RDongre
You can pass a name or a group of names via model to be assigned in partial views. You pass all the data your partial view needs. Or do you mean something else? Can you please elaborate then?
Developer Art
Right, strongly typed model allows me to pass named data to user control agree. What i had mentioned was to be able to capture by id in Javascript something like UserCtl1.Name, UserCtl.Phone and to apply some HTML attributes to them. Also in your previous answer you had mentioned about partial-views; do they bind us to use only AJAX based data retrival? What happens when we submit a button on user control? It always hit POST method of parent page and not partial-view actionresult right?
RDongre
Partial view rendered output is simply merged into the host page markup. After the pages has been rendered, there is no more separation between page and partial views - it's just one piece of HTML. You can do with it whatever you need. JavaScript won't see the difference.
Developer Art
When a submit button rendered on a partial view is enacted, the form encompassing this button will be posted back. How the form is created, by the partial view itself, in its parent partial view (if any) or in the host page, doesn't matter. After the page is rendered, it is single piece of HTML.
Developer Art
What action of what controller a POST will skim, will depend on the routes definition. You can also set a different POST url in the form to explicitly ask for a specific action of a specific controller. That's just how you design the application.
Developer Art