1

I wrote simple form, which have simple form with button. Onclick with javascript method on this button should fire a ajax which should go to '/Parser/GetParseData'. But I always got error without any detail (always run request.fail).

I don't know what is problem. General My aim is fire ajax with data from form and after success (and getting answer) I want to put received string in $('#result').

Could you help me?

Ok my View:

@using Project.Domain.Entities
@model ParserHelper

@{
    ViewBag.Title = "Project";
}
<h2>simple form asp.net mvc 5</h2><br/>

@using (Html.BeginForm())
{
<fieldset>
    <div class="editor-field">
        @Html.TextAreaFor(Model => Model.InputData)
    </div>
    <div class="radio-field">
        @Html.RadioButtonFor(Model => Model.TypeOfResult, "xml") @Html.Label("Xml") <br/>
        @Html.RadioButtonFor(Model => Model.TypeOfResult, "csv") @Html.Label("Csv")
    </div>
    <button onclick="submitTest()">Request!</button>
</fieldset>
}

<div id="result">
</div>

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
        function submitAlert() {
            alert("testtest");
        }

        function submitTest() {
            event.preventDefault();

            var data = {
                InputData: $('#InputData').val(),
                TypeOfResult: $('#TypeOfResult').val()
            };

            request = $.ajax({
                url: "/Parser/GetParseData",
                type: "post",
                formData: data
            });

            request.done(function (response, textStatus, jqXHR) {
                alert("Hooray, it worked!");
                $('#result').text('Its worked:' + response);
            });

            request.fail(function (jqXHR, textStatus, errorThrown) {
                alert(
                    "The following error occurred: " +
                    textStatus, errorThrown
                );
                $('#result').text('Sorry, error');
            });
        }
</script>

My model is:

public class ParserHelper : IFormHelper
{
    public string ResultAfterParse { get; set; }

    public string InputData { get; set; }

    public string TypeOfResult { get; set; }
}

Finally here is my controller metho in: ParserController : Controller:

[HttpPost]
public string GetParseData(IFormHelper formData)
{
    return "ANSWER!!!";
}

I also added this as Routing:

routes.MapRoute(
            name: "PostData",
            url: "{controller}/{action}",
            defaults: new { controller = "Parser", action = "GetParseData"  
});

If you need any more information let me know - I will edit this post.

SOLVED.

2 Answers 2

2

Your POST method signature needs to be

public string GetParseData(ParserHelper formData)

You cannot use an interface as a parameter. The DefaultModelBinder initializes objects using Activator.CreateInstance() and an interface cannot be initialized, only its concrete types.

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

2 Comments

But I use IOC Container - I mean Ninject. Even so I have to put strong typped var?
Yes (unless you were to create your own custom ModelBinder)
1

If the error is 404 not found, then the problem could the URL is not correct because of the hard-coding the URL which fails mostly if client side code is moved to a separate js file which resides in some sub folder in that case the path of URL becomes invalid, you should be using Url.Action method to generate the correct url like:

url: '@Url.Action("GetParseData","Parser")'

Hope it helps!

1 Comment

I tried that - but it doesn't help. I only know that it is internal server error

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.