0

I try to passing a parameter to simply action that return a partial view in a popup, but I can't to passing the parameters, and as if it did not arrive at the action.

I don't understand why???

I use .net core 2.

this my code.

The action

[HttpPost]
public IActionResult Detail([FromBody] string tableName, [FromBody] string code)
{
    var row = tableBLL.GetAllByCode(tableName, code);
    return PartialView("~/Views/Table/Detail.cshtml");
}

The jquery function. The value patamenter are hard coded to try the operation, after I should recover them from the table

 $("#dt_basic a.details").click(function() {
                    $.ajax({
                        method: "POST",
                        url: "/Table/Detail",
                        data: JSON.stringify({
                            "tableName": "IN_COMAGREE",
                            "code": "1"
                        }),
                        //data: '{code: "' + codeId + '" }',
                        contentType: "application/json",
                        //dataType: "html",
                        success: function (response) {
                            $('#dialog').html(response);
                            $('#dialog').dialog('open');
                        },
                        failure: function (response) {
                            alert(response.responseText);
                        },
                        error: function (response) {
                            alert(response.responseText);
                        }
                    });
                });

The Partial view ( very simple in this moment )

 <form asp-action="" class="smart-form">
        <header>
            Name table
        </header>

        <fieldset>
            <section>
                <label class="label">Cod</label>
                <label class="input">
                    <input type="text" class="input-xs">
                </label>
            </section>

            <section>
                <label class="label">Descr</label>
                <label class="input">
                    <input type="text" class="input-xs">
                </label>
            </section>

        </fieldset>
    </form>

Point of the call to the function

<table id="dt_basic" class="table table-striped table-bordered table-hover" width="100%">
                <thead>
                    <tr>
                        @foreach (DataColumn col in Model.Columns)
                        {
                            <th>@col.ColumnName</th>
                        }
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (DataRow row in Model.Rows)
                    {
                        <tr>
                            @foreach (DataColumn col in Model.Columns)
                            {
                                <td>@row[col.ColumnName]</td>
                            }
                                <td>

                                    <a href="#" class="details"> Edit @row[0]</a>
                                    <a href="#" class="btn bg-color-red txt-color-white btn-xs"> Delete @row[0]</a>
                                </td>
                        </tr>
                    }
                </tbody>
            </table>
Cicciux
  • 177
  • 4
  • 19
  • Remove `JSON.stringify(` – freedomn-m Jan 10 '18 at 10:59
  • Possible duplicate of [Why do we have to specify FromBody and FromUri?](https://stackoverflow.com/questions/24625303/why-do-we-have-to-specify-frombody-and-fromuri) – freedomn-m Jan 10 '18 at 11:05
  • You can only have one `[FromBody]` so use a custom class, see here for more info: https://stackoverflow.com/a/41027149/2181514 – freedomn-m Jan 10 '18 at 11:06
  • thanks for reply... I try to remove FromBoby and JSON.stringify( but don't work.. :-(((( – Cicciux Jan 10 '18 at 11:13
  • I don't use a webservice my is a action in a controller – Cicciux Jan 10 '18 at 11:21
  • The principle is the same. You can only have one `[FromBody]` so you need to create a model with the two strings as properties. – freedomn-m Jan 10 '18 at 11:31
  • Try this: Change your action signature to `public IActionResult Detail([FromBody] string tableName) {` (ie remove the other string) and add the `JSON.stringify(` back in to see what you get - you should get `tableName` as a json string. – freedomn-m Jan 10 '18 at 11:32
  • public IActionResult Detail([FromBody] string tableName) { result partialview (... ) } and my Ajax : $.ajax({ method: "POST", url: "/Table/Detail", data: JSON.stringify({ "tableName": "IN_COMAGREE" }), Don't work .. it' unbelievable – Cicciux Jan 10 '18 at 11:38
  • What do you get in the `error:` callback? – freedomn-m Jan 10 '18 at 11:43
  • Do you have a breakpoint on the MVC action and running debug mode? – freedomn-m Jan 10 '18 at 11:43
  • yes I Run in debugn mode and I put a break point in action and the parameter is null ... I don't obtain no error in call back but only that no value to parameter arrive. have You try my code? – Cicciux Jan 10 '18 at 12:07
  • And your click handler is being called? `$("#dt_basic a.details").click(function() { alert("click"); });` (ie is the `details` anchor added dynamically?) – freedomn-m Jan 10 '18 at 12:10
  • [_italic_] detail [_italic_] not is added dynamicaly is in layout... Is when i click on [_italic_] detail [_italic_] i view the alert – Cicciux Jan 10 '18 at 12:15
  • @freedomn-m can you do a example how this should be? – Cicciux Jan 10 '18 at 12:17

1 Answers1

1

ASP.NET Core documentation stated that,

There can be at most one parameter per action decorated with [FromBody]. The ASP.NET Core MVC run-time delegates the responsibility of reading the request stream to the formatter. Once the request stream is read for a parameter, it's generally not possible to read the request stream again for binding other [FromBody] parameters.

You need to create a model

public class TableData
{
    public string tableName { get; set; }
    public string code { get; set; }
}

Then in your action method use it like this

public IActionResult Detail([FromBody] TableData table)
Ahmar
  • 3,717
  • 2
  • 24
  • 42