Click here to Skip to main content
15,071,921 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
View
<pre lang="HTML">@model SendAFaxWeb.Models.Send
//view start here
<body>
    <div>
        <h2>Test Upload File</h2>
        <form action="@Url.Action("Index", "Home")" id="form" method="post" enctype="multipart/form-data">
            @Html.AntiForgeryToken()
            <div class="form-group">
                <label>Fax Number:</label>
                @Html.TextBoxFor(m => m.Recipients[0].Number)
             </div>

            <div class="form-group">
                <label>Select File:</label>
                <input type="file" name="files" id="file" multiple="multiple" onchange="this.form.submit();" />
            </div>

            <div>
              @if (Model != null)
                 {
                   foreach (var item in Model.Documents)
                    {
                       <li>FileName: @item.Name</li>
                    }
                  }
             </div>
    </form>

    <input type="submit" name="send" value="Send" id="btnSend" />

    </div>
</body>


Javascript

HTML
<script type="text/javascript">
    $(document).ready(function () {
     $("#btnSend").click(function (e) {
        alert("button click");
        e.preventDefault();

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

</script>


Controller

C#
public ActionResult Send(Send contact)
{
    //some code here
}


What I have tried:

I tried to pass model by using javascript to the controller, but its not working. I think got something wrong with my javascript. Can anyone help me?
Posted
Updated 11-Aug-16 21:55pm
Comments
F-ES Sitecore 12-Aug-16 6:10am
   
Define "not working"?

One issue I can see right away is that you are serialising the data in the model as it is when the page loads;

var model = @Html.Raw(Json.Encode(Model))

You're not serialising the data that is in the form when it is submitted.

This is fairly basic MVC stuff, look at tutorials on calling MVC actions via jquery ajax and you'll find lots of examples on how to do it.

1 solution

   
Comments
VICK 12-Aug-16 8:18am
   
My 5.
Animesh Datta 12-Aug-16 8:33am
   
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900