开发者

How do I access a variable value assigned to an HTML.Hidden variable in a MVC Controller Action Method

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 (DevExpre开发者_如何学JAVAss 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?


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.


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜