Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating a feedback form by FormMethod.Post method in mvc. Now, in my form data is saving but after save the data page is refreshed so that user cant know that data is successfully inserted or not. So, for thats why I want to alert the javascript and refresh the current page.

I have done many things but didnt work anything.

So, let I am showing you the last thing I tried that in this code.

Here is my view

<script>
@ViewBag.scripCall

    function Callback() {
        alert("Callback function is working");
    }
</script>

@using (Html.BeginForm("Create", "FeedBack", FormMethod.Post, new { enctype = "multipart/form-data" }))
{   
  <div class="container">
        <div class="row">
            <div class="col-sm-3">
                <img src="~/images/default.png" style="margin:10px" height="200" width="200" id="imagePreview" />
                <input type="file" name="ImageUpload" accept="image/jpeg, image/png" id="ImageUpload"
                    onchange="ShowImagePreview(this, document.getElementById('imagePreview'))" class="control-label" />
            </div>
            <div class="col-sm-2">
                @Html.LabelFor(model => model.FullName, new { @class = "control-label" })
            </div>
            <div class="col-sm-8">
                @Html.TextBoxFor(model => model.FullName, new { @class = "form-control", placeholder = "Enter The Name" })
            </div>
            <div class="col-sm-9">
                <br />
                </div>
            <div class="col-sm-1">
                @Html.LabelFor(model => model.Occupation, new { @class = "control-label" })
            </div>
            <div class="col-sm-8">
                @Html.DropDownList("Occupation", ViewData["occupationList"] as SelectList, new {@class="control-label", style="width:250px;" })
            </div>
            <div class="col-sm-9">
                <br />
                </div>
            <div class="col-sm-3">
                @Html.LabelFor(model => model.UserFeedBack, new { @class = "control-label" })
            </div>
            <div class="col-sm-8">
                @Html.TextBoxFor(model => model.UserFeedBack, new { @class = "form-control", placeholder = "Enter FeedBack", style="height:250px;" })
            </div>
            <div class="col-sm-3">
                    <input type="submit" id="btn_save" class="button" value="Save" />
                </div>
        </div>
      <div class="row">
          <span class="control-label" style="color:red">Note : Please do not upload DSLR photo. It does not support. Thank You...!!!</span>
          </div>
    </div>   
}


Here is my controller

public ActionResult Create(FeedBackViewModel feedbackViewModel)
{
    try
    {
        HttpPostedFileBase file = Request.Files["ImageUpload"];
        if (file != null)
        {
            string fileName = Path.GetFileNameWithoutExtension(feedbackViewModel.ImageUpload.FileName);
            string extension = Path.GetExtension(feedbackViewModel.ImageUpload.FileName);
            fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
            feedbackViewModel.ImageUpload.SaveAs(Path.Combine(Server.MapPath("~/images/"), fileName));
        }

        ContentRepository service = new ContentRepository();
        int i = service.CreateFeedBack(file, feedbackViewModel);
        if (i == 1)
        {
            ViewBag.scripCall = "Callback();";
            return View();

            //return Content("<script language='javascript' type='text/javascript'>alert('Thanks for Feedback!');</script>");    
        }
        else
        {
            return Content("<script language='javascript' type='text/javascript'>alert('Sorry, Something went wrong. Please Try Again...!!!');</script>");
        }
    }
    catch (Exception ex)
    {
        return View();//"Error", new HandleErrorInfo(ex, "StaffRegistration", "Create"));
    }
}


Now, in this code the latest thing I triedto show the alert by @ViewBag.scripCall and also the refresh the current page but it also not working.

Thank You.

What I have tried:

I tried many of thing but didnt work and the latest thing I tried i.e. by
@ViewBag.scripCall
and also didnt work
Posted
Updated 20-Nov-17 22:17pm
Comments
massimiliano aronica 13-Nov-17 7:38am    
Well, I would insist on ViewBag solution, but I don't see any model passed to the view in your Return View() statements. You can also try to set a new property in your model, representing the javascript command in a string, and then printing it in the page.
I would also move the Script section after the form, or I'm afraid the alert would raise on a blank page.
Anyway look at the source of the page when you expect to get the alert, and check the resulting JS code

1 solution

you did not display your namespace

On the top of the view page add your namespace with modelname
@model namespacename.Models.FeedBackViewModel
 
Share this answer
 

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