1

I have a text file and when users upload the file, the controller action method parses that file using state machine and uses a generic list to store some values. I pass this back to the page in the form of a hidden field. Users can then click on a link which invoked a JS modal dialoag box and they could see the list and add comments for each item in the list. When they click on the link I am trying to post to an action method which would take that hidden field and do something with it and render a partial view. The problem is when i post to this action method, this field is being passed as null.

here is my code

@Html.HiddenFor(model => model.ExceptionString)



if (Model.ExceptionString != null)
           {
               if (Model.ExceptionString.Count > 0)
               {
        <div class="bodyContent">
            <span class="leftContent">
                @Html.Label("Test Exceptions")
            </span><span class="rightContent"><span id="TestExceptionChildDialogLink" class="treeViewLink">Click
                here to View Test Exceptions</span>
                <br />
                <span id="TestExceptionDisplay"></span>
                @Html.HiddenFor(model => model.ExceptionString)
                <input id="ExceptionString" type="hidden" value="@Model.ExceptionString" />
            </span>
        </div>
               }
           }

<div id="testExceptiontreeview" title="Dialog Title" style="font-size: 10px; font-weight: normal;
    overflow: scroll; width: 800px; height: 450px;">
    <div id="testExceptions">

    </div>
    <div id="inputTestExceptions" style="display: none;">
    </div>
</div>


  var runlogTestExceptionUrl = '@Url.Action("ListTestExceptions", "RunLogEntry")';

JS FILE

 $("#inputTestExceptions").load(runlogTestExceptionUrl, { ExceptionStrings: $("#ExceptionString").val() });

Controller action

[HttpPost]
        public ViewResult ListTestExceptions(List<string> ExceptionStrings)
        {

Any ideas as to why exception string list is null while being passed by the JS to the abive action method?

1 Answer 1

1

$("#ExceptionString").val() is going to return a single string. This means your call to .load is just posting a single name/value pair to your MVC application.

You need it to be posting a collection of name/value pairs. The name will be the same: the name of the collection with an indexer. The value will be the exception string. This is going to take some significant refactoring.

Here's basic way this could work:

View code:

<form action="@Url.Action("ListTestExceptions", "RunLogEntry")" TYPE="POST">
@{ int counter = 0;}
@foreach(var exception in Model.ExceptionString)
{

  <input id='@("ExceptionString["+counter+"]")' type="hidden" value='@exception' />
  @{ counter =counter + 1; }
}
</form>

JS code:

$.ajax({
        url: runlogTestExceptionUrl,
        data: $(this).serialize(),
        success: function(data) {
             $('#inputTestExceptions').html(data);
        }
});

Controller action is unchanged:

[HttpPost]
    public ViewResult ListTestExceptions(List<string> ExceptionStrings)
    {

Obviously this isn't going to integrate instantly with your code. But this would be the way to post data from a form to a single collection variable in MVC.

Note: I'm a little rusty on the razor syntax so there may be some little syntax issues. I'll fix them as I find them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.