Pass HTML Content from View to Controller using jQuery Ajax



This blog is going to explain how to send a large amount of text content or HTML content from the view to the controller using jQuery Ajax.

Ajax means Asynchronous JavaScript and XML. It helps you to load the server data without refreshing the browser. So you can refresh part of the web page without loading the entire page. jQuery Ajax performs asynchronous Ajax requests.

The following is a view code. Here I have included the Google jQuery CDN (If you want you can download the jQuery library and include the link). Also, I have added some text boxes and textarea to collect the input values.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<table class="width-hundred-percent">
    <tr>
        <td> ID: </td>
        <td> <input id="idTextBox" type="text" style="width:100%" /> </td>
    </tr>
    <tr>
        <td>Subject</td>
        <td><input id="subjectTextBox" type="text" style="width:100%" /></td>
    </tr>
    <tr>
        <td valign="top">Content: </td>
        <td><textarea id="contentTextArea" style="margin: 0px;width: 500px;height: 300px;"></textarea>
        </td>
    </tr>
    <tr>
        <td colspan="2" align="right"> <button id="saveButton">Save</button></td>
    </tr>
</table>

Here I'm going to pass the values using the complex parameter. So I have created a class called ContentModel.

public class ContentModel
{
    public int ID { get; set; }
    public string Subject { get; set; }
    public string Content { get; set; }
}

The below is jQuery code, The jQuery click event is used to send values to the controller by clicking the button. $("#saveButton").click() bind the event handler to the javascript click event. When you click on the save button it will trigger the event.

When the event triggers, first the object will create with the input values.
The first input value is the ID integer, so it is converted to an integer using the parseInt
function. Using JSON.stringify(obj), the object is converted to a JSON string and passed to the /Home/Save controller action

<script>
    $("#saveButton").click(function () {
        obj = {
            ID: parseInt($("#idTextBox").val()),
            Subject: $("#subjectTextBox").val(),
            Content: $("#contentTextArea").val()        
        };

        $.ajax({
            url: '/Home/Save',
            contentType: 'application/json; charset=utf-8',
            type: 'POST',
            data: JSON.stringify(obj),
            cache: false,
            success: function () {
                alert("Saved");
            },
            error: function () {
                alert("Error");
            }
        });
    });
</script>

The below one is controller code. In the Save action [FromBody] is added to the parameter, so it will take the parameters from the body of the HTTP request.

public IActionResult Index()
{
    return View();
}

public IActionResult Save([FromBody]ContentModel content)
{
    return View(content);
}

The following is a screenshot of the code above.


The visual studio provides data tips to see the data value when the breakpoint is set. The following is a visual studio data tips screen. Here you can see the values that passed from view.



I hope this blog will help you. Keep coding

Comments

Popular posts from this blog

Entity Framework Core (EF) with SQL Server LocalDB

SignalR with JavaScript in ASP.NET Core MVC

Localization in ASP.NET Core MVC with Example