tags:

views:

30

answers:

2

I am writing my first ASP.Net webpage and using MVC.

I have a string that I am building in a partial view with a grid control (DevExpress MVCxGridView). In my partial view I am using a HTML.Hidden helper as shown below.

    ' Create a hidden variable to pass back a comma-delimited string
Response.Write(Html.Hidden( "exclusionList", Model.ExclusionList))

The value of of this hidden element is assigned in client side javaScript:

 exclusionListElement = document.getElementById("exclusionList");
 // ... 
 exclusionString = getExclusionString();
 exclusionListElement.value = exclusionString;

This seems to work without problem.

In my controller action method:

        <AcceptVerbs( HttpVerbs.Post )> _
    Public Function MyPartialCallback(updatedItemList As myModel) As ActionResult

        Dim myData As myModel = GetMyModel()

        Return PartialView( "MyPartial", myModel.myList )
    End Function

The updatedItemList parameter is always nothing and exclusion list exists no where in the Request.Forms.

My questions are:

  1. What is the correct way to use Html.Hidden so that I can access data in a MVC Controller Action method.

  2. Is adding "cargo" variables to Request.Form the best and only way to send data back to a server side MVC Controller Action method? It just seems like twine and duct-tape approach. Is there a more structured approach?

A: 

If you need to get the exclusionList variable back, you just need to add a property to your view model that matches that name exactly. Make sure it is of the correct type (string it looks like in this case) and then it should auto populate that property in the view model for you.

And yes, there is no need for the Response.Write call. Instead just use the Html.HiddenFor(...) helper in your view.

NickLarsen
I tried this too - but to no avail. I think the only way to do this is with an ajax call to a controller method. Currently in my ascx (partial view) I define an Html.ActionLink that calls a client side function that calls a javaScript function to update the partial and returns true to proceed to a server side controller method. Since I really don't want a post-back, I need to call a controller method via ajax. I wonder if Ajax.ActionLink will allow me to call the same javaScript method followed by a controller method (with no return view...)?
Doug Kimzey
If you are making a call to the server via Ajax, you will need to populate the data object for the request yourself. That isn't handled by the browser unless you are posting back... or I suppose if you are using a JavaScript framework which handles it for you. Assuming you are using jQuery, just pass the request an object with a property named `data` and make sure that the properties of your data match your controller action.
NickLarsen
A: 

Look at the generated HTML. Note down the name attribute of the hidden field. Use this name as action parameter name:

Public Function MyPartialCallback(exclusionList As string)
Darin Dimitrov
Thanks for the reply, I tried this suggestion but the result is still that exclusionList = Nothing in the action method.
Doug Kimzey