0

How I can pass an object of the model from view to controller using JS function?

<div class="col-md-offset-2 col-md-10">
            <button type="button" class="btn btn-secondary" onclick="BulkUpdate(@Model)">Bulk Update</button>
        </div>

 function BulkUpdate()
    {
        $('#myModal').modal('show');
    }
4
  • Try: BulkUpdate('@Model') Commented Dec 23, 2019 at 7:47
  • Does this answer your question? Asp.net mvc passing a C# object to Javascript Commented Dec 23, 2019 at 7:48
  • What about write hidden objects on view to use them on js? Commented Dec 23, 2019 at 7:56
  • Your model is C# object (server side)and your script will run at client side. How can you access the c# object at client side? Please, elaborate about your requirement so that we can help you. Commented Dec 23, 2019 at 9:19

1 Answer 1

0

Use ajax to hit your controller method. see the example:

JS

function BulkUpdate()
{
   var model = @Html.Raw(Json.Encode(youModel))
   $.ajax({
       type: 'post', //GET
       url: '@Url.Action("Send", "Home")',
       data: JSON.stringify({ contact: model }),
       contentType: 'application/json; charset=utf-8',
       dataType: "json",
       success: function (data) {
           alert(data);
       }
   });

}

Controller

public ActionResult Send(Send contact)
{
    //some code here
}
Sign up to request clarification or add additional context in comments.

6 Comments

please let me know if you feel have confusion.
Thanks, Ajay Kumar I am working on it if I have any issue I will contact you thank you so much
bro can you please share your contact number?
i dont think its a good idea, but we can do a discussion chat separately using stackoverflow discussion chat. check it somewhere in your question page.
Bro there is no option for chat ??
|