views:

261

answers:

1

Hi,

Using ASP.Net MVC 1.0 I have a form with some input control on it. One of them is a dropdown (select). When this dropdown gets changed by the user I like to update a DIV-tag using RenderPartial() or something like this.

My view currently look like this:

<% using (var form = Html.BeginForm())
   { %>
       <label for="FieldIdentifier">Identifier:</label>
       <%=Html.TextBox("FieldIdentifier", Model.FieldIdentifier)%>
       ...

       <label for="DataType">DataType:</label>
       <%=Html.DropDownList("DataType", Model.AvailableDataTypes)%>

       <div id="DataTypeOptions">
          <% Html.RenderPartial("FieldDataTypeOptions", Model); %>
       </div>
       ...

In Webforms the functionality I am looking for could be done with an UpdatePanel around the dropdown and the DIV. Can Ajax.BeginForm() help here or is JScript on the "OnChange" client event needed? If so, how does one update the DIV-part of the view using JScript?

Any help would be great!

Cheers, Marc

A: 

If you want to update the DIV based on the change in the dropdown, then you'll need an action on the controller that can return just the correct partial view based on the dropdown value. I would suggest using jQuery to add an change handler for the dropdown and call the action to get the partial via jQuery load.

$(function() {
    $("#DataType").change( function() {
        $("#DataTypeOptions").load( '<%= Url.Action( "GetDataTypeOptions" ) %>?dataType=' + $(this).val() );
    });
});

(note change in div name -- I think you have a typo)

where you have an action on the same controller as

public ActionResult GetDataTypeOptions( string dataType )
{
    var model = ... uses dataType to get model ...

    return Partial( "FieldDataTypeOptions", model );
}

If your "Get" method needs more inputs, simply add parameters to the method and construct the parameters in the load call from the values of the additional input elements.

tvanfosson
Looks exactly what I am looking for. Just one noob question: Where to put the above JQuery script into? In a script section at the top of the markup?
Marc
Typically I put my script tags just prior to the end of the body element so that they are loaded last. Most (all?) browsers handle javascript requests sequentially so that any code is executed in the order that it appears. Loading at the beginning will delay any other elements of the page from loading in parallel, potentially slowing the overall page load.
tvanfosson
sounds good to me. still try to get the jscript in place. as a webforms-developer i did not yet do a lot with jscript uh ;) just added at the end of my body your jquery script like this: <script type="text/jscript" language="jscript"> $(function() { ... </script>. That gives me a "Microsoft JScript runtime error: Object expected" error. What do I miss? mhhh
Marc
ups, just found that i did not yet include the jquery script at all :-/ now i don't get errors but is doesn't get back to the controller action. have to debug a little further...
Marc
got everthing working now.in case others stuck as well: my final error was that i flagged my controller action with an [Authorized] attribute as i did most of my other actions. the authorized attribute seems not to be supported with this kind of callbacks.
Marc
It should be as long as the auth cookie hasn't expired by the time that the request is made -- and assuming that the user is logged in.
tvanfosson