Search code examples
c#asp.net-mvc-4model-view-controllerasp.net-identity

transfer data from view to controller


How do this. I want to transfer data What I did:

js

    $("#add-ling-user-couch").click(function () {
    var CoucnId = $(this).data('couch-id');
    var data = { "CoucnId": curentUserId };
    $.ajax({
        url: '/AppUser/AddLink',
        type: "POST",
        data: data,
        success: function (result) { console.log(result) }
       // dataType: dataType
    });
});

cshtml

 @foreach (var a in Model)
    {
        <tr>

            <td>@a.FirstName</td>

            <td>
                <div class="btn-group" role="group">
                    @Html.ActionLink("edit", "Edit", new { id = a.Id }, new { @class = "btn btn-secondary" })
                    @Html.ActionLink("Del", "Delete", new { id = a.Id }, new { @class = "btn btn-secondary" })
                    <a id="add-ling-user-couch" data-couch-id="@a.Id">Add Link</a>
                </div>

            </td>

        </tr>

Contoller

        [HttpPost]
    [AllowAnonymous]
    public JsonResult AddLink(AddLinkViewModel model)
    {


        return new JsonResult()
        {
            Data = new { success = true, message ="Success" }    
        };
    }

I get some problem use it.

How can to do it other wise? My way create more problem than it solve

it would work but I dont find it's correct

<form method="post">
                                <input type="hidden" value="@a.Id" name="CoucnId" />
                                <input type="submit" value="Sender" />
                            </form>

Solution

  • javascript:

    $.post("/AppUser/AddLink", { CoucnId: curentUserId }, function (data) {     
       
        });
    

    then in controller return partialview with modle:

     [HttpPost]
        [AllowAnonymous]
        public JsonResult AddLink(int CoucnId)
        {
          fetch data and fill modle:
          return partialview("viewname",model);
        }