0

I'm making a post request from on view so that I don't see the parameters on the URL and I can tell it is passing the appropriate parameters to controller for the request but it does not display the appropriate view from that controller.

Calling view

            @Ajax.ActionLink("Work1", "NewIndex", "WorkItems",
            new
            {
                eventCommand = "createforrig",
                //eventArgument1 = @item.Id,
                eventArgument2 = @item.Id
            },
            new AjaxOptions
            {
                HttpMethod = "POST"
            })

WorkItems Controller method

    [HttpPost]
    public ActionResult NewIndex(NewWorkItemViewModel vm)
    {
        vm.IsValid = ModelState.IsValid;
        vm.HandleRequest();

        if (vm.IsValid)
        {
            // NOTE: Must clear the model state in order to bind
            //       the @Html helpers to the new model values
            ModelState.Clear();
        }
        else
        {
            foreach (KeyValuePair<string, string> item in vm.ValidationErrors)
            {
                ModelState.AddModelError(item.Key, item.Value);
            }
        }

        return View(vm);  
    }

Putting a breakpoint on the last Return View(vm) confirms it is being called but the browsers does not update to display the workItems view.

Suggestions on why the browser is not being updated to display the appropriate view.

user3502865
  • 143
  • 7

1 Answers1

0

You're making an ajax post, the newly rendered view is being returned by the server if you were to look in the network console in your browser. Add a success callback. Either assign a callback to handle the response or use the UpdateTargetId property in your AjaxOptions

@Ajax.ActionLink("Work1", "NewIndex", "WorkItems",
            new
            {
                eventCommand = "createforrig",
                //eventArgument1 = @item.Id,
                eventArgument2 = @item.Id
            },
            new AjaxOptions
            {
                HttpMethod = "POST",
                OnSuccess = "AjaxSuccess", //handle with callback
                UpdateTargetId = "MyElementID" //update html element
            })

if you choose to use OnSuccess then in javascript

function AjaxSuccess(data){
    //handle response
}

AjaxOptions properties and usage can be found here

EDIT You could use javascript to submit a form when a link is clicked, put a form somewhere in your code and hide it.

@using (Html.BeginForm("NewIndex", "WorkItems", FormMethod.Post, 
new { class = "hidden", id = "postForm" } ))
{
   <input type="hidden" name="eventCommand" value="createforrig" />
   <input type="hidden" name="eventArgument2" value="@item.Id" />

   <input type="submit" value="link text" id="submitForm"/>
}

then change your @Ajax.ActionLink... to

@Html.ActionLink("Work1", "NewIndex", "WorkItems", new { id = "postLink"})

and if you're using jQuery

<script>
$(function(){
  $('#postLink').click(function(e)
  {
    e.preventDefault();
    $('#postForm').submit();
  });
});
</script>

and don't forget to hide the form in css

.hidden { display:none;}
Mark F
  • 176
  • 7
  • Perhaps I'm using the wrong method because ultimately what I'm trying to do is call have a link to another page using a post request so I don't see the parameters. So this request is intended to redirect to a different URL. – user3502865 Apr 27 '17 at 22:15
  • You would need to make the link a form and style it and the input button to look like an `` tag with css. Similar to this http://stackoverflow.com/questions/3915917/make-a-link-use-post-instead-of-get – Mark F Apr 27 '17 at 23:56
  • Mark F - thanks for the response. After a bit of playing around I got this working and added the additional form onto the view and modified the links to set the properties in this form. Its a little bit messy in my opinion that I need to add the form to every view to make this happen but its workable. – user3502865 Apr 28 '17 at 15:34
  • Ideally it would be nice to have the click functionality create the form dynamically on the fly that way it would simply be adding the click method rather than depending upon extra html present. But the solution provided works. Thanks for the solution. Is this typically similar to how most websites handle links with data not wishing to reveal the data on the URL ? – user3502865 Apr 28 '17 at 15:34