Link to home
Start Free TrialLog in
Avatar of Crystal Rouse
Crystal RouseFlag for United States of America

asked on

JsonResult asks the user to download the json file when submitting the form?

Using JsonResult causes the form submission to ask the user if they want to download the json file?

My controller is:

 [HttpGet]
        public ActionResult Edit(int id)
        {
         
            tbl_MyTable tbl = (from x in DB.MyTable
                             where x.ID == id
                             select x).Single();

            return View(edit);

        }

[HttpPost]
        public JsonResult Edit(List<newList> formData)
        {
            var result = new { Success = "", Message = "" };
            try
            {
                   foreach (var item in formData)
                {
                    tbl_MyTable  Data = (from x in DB.tbl_MyTable
                                             where x.ID == item.ID
                                             select x).Single();

                    Data.Type = item.Type;
                    Data.Name = item.Name;
                    Data.Version = item.Version;
                    Date = item.Date;
                }

                DB.SaveChanges();
                TempData["msg"] = "<script>swal('Item successfully updated!');</script>";

            }

            catch (Exception ex)
            {
                Utility util = new Utility();
                string error = util.innerExcep(ex);
                result = new { Success = "False", Message = error };
                return Json(result);
            }

            result = new { Success = "True", Message = "Success" };
            return Json(result);

        }
        }

My View is: (Shortened snippet below)

@model master.Models.tbl_MyTable
@{
    ViewBag.Title = "Edit Item";
    ViewBag.submittable = true;
    master.Models.SelectLists sl = new master.Models.SelectLists();
}
   
@using (Html.BeginForm())
{
    @Html.HiddenFor(x => x.ID)

<div class="row" style="margin-top: 0;">

                        <div class="col-xs-3">
                            @Html.Label("Type")
                            @Html.DropDownListFor(x => x.Type, sl.getTypes(), new
                       {
                           @type = "drop",
                           @style = "width: 255px;",
                       })                      
                        </div>
<div class="col-xs-8 col-xs-offset-1">
                            @Html.Label("Name")
                            @Html.TextBoxFor(x => x.Name, Model.Name, new
                       {
                           @class = "form-control",
                           @maxlength = "255",
                       })                  
                        </div>
                    </div>

<div class="col-xs-3">
                                @Html.Label("Date")
                                @Html.TextBoxFor(x => x.Date, Model.Date.ToString(), new
                           {
                               @class = "form-control",
                               @type = "date",
                               @maxlength = "15",
                           })
                            </div>

 <div class="row">
                        <div class="col-xs-12">
                            <button type="submit" class="btn btn-success" style="float: right;" onclick="javascript: submitForm();">
                                Submit Changes
                            </button>
                        </div>
                    </div>

<script type="text/javascript">
    function submitForm() {

        if ($("#Type").val() != '' && $("#Name").val() != '' && $("#Date").val() != '' ) {
            var type = $("#Type").val();
            var name = $("#Name").val();
            var date = $("#Date").val();

            var formData = [
                {
                    Type: type, Name: name,
                    Date: date,
                    ID: $("#ID").val(),
                }
            ];

            $.post('@Url.Action("Edit", "MyController")', { "formData": formData })
                .done(function (data, status) {
                    if (data.Success == "True") {
                        var link = '@Url.Action("MyList", "MyController")';
                        window.location.href = link;
                    }
                    else {
                        swal({
                            title: "Error",
                            text: data.Message,
                            type: "error"
                        }, function () {
                            var link = '@Url.Action("Edit", "MyController")';
                            window.location.href = link;
                        });
                    }
                });
        }
        else {
            swal({
                title: "Error",
                text: "Please complete required fields.",
                type: "error"
            }, function () {
                url = '@Url.Action("Edit", "MyController", new { id = Model.ID })';
                    window.location.href = url;
                });
        }
    }

</script>


@if (!string.IsNullOrEmpty(ViewBag.Error))
{
    <script type="text/javascript">
            swal({
                title: "Error",
                text: "@ViewBag.Error",
                type: "error"
            }, function () {
                url = '@Url.Action("Edit", "MyController", new { id = Model.ID })';
                window.location.href = url;
            });
    </script>
}
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Crystal Rouse

ASKER

Thanks! That is what was missing!
You are welcome.