7

I have a form with two fields which will pass values to a stored procedure. The stored procedure will return 0 or 1.

If 0 the user is not eligible to see the data. If 1, he is. Then I want to show the details in the same page which I have submitted. I am using MVC 4 razor.

Please give some idea how to achieve this. I am new to MVC.

Mathieu K.
  • 283
  • 3
  • 14
Sachu
  • 7,555
  • 7
  • 55
  • 94
  • Based on your result you can render partialview.If sp returned 1 render partialview otherwise don't do anything. – Mairaj Ahmad Apr 14 '15 at 10:27
  • why dont you use Ajax call? with submit you have to reload your page! with a simple 'button' and 'onclick()' you can append results in your page using 'javascript' – Khaled triki Apr 14 '15 at 10:27

3 Answers3

8

You can use javascript ajax to post your from. using this you can get the data through ajax without refreshing the page and then you can use your ajax result to display the data.

HTML code

<form id="MyForm"  method="post">
<input type="text" name="value1"/>
<input type="text" name="value2"/>
<input type="submit" name="Submit" value="Submit" />
</form>

Controller code

public class HomeController : Controller
{
    [HttpPost]
    public ActionResult MyAction(MyViewModel model)
    {
        var result = ResultOfStoredPrcedure();
        return Content(result);
    }
}

javascript code

<script >

 $(document).ready(function () {

    $('#MyForm').submit(function (e) {

        var formData = new FormData(this);

        $.ajax({
            url: '@Url.Action("MyAction", "Home")',
            type: "POST",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: formData,
            contentType: false,
            processData: false,
            success: function (result) {
             // here in result you will get your data
            },
            error: function (result) {

            }
        });
        e.preventDefault();
    });
});
</script>
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Firoz Jafri
  • 168
  • 1
  • 8
4

If you want you can utilise MVCs Ajax Helper, and use the Ajax.BeginForm(), or use javascript and a standard form to post. Whatever the choice, in your Action just return a View.

If you use the Ajax.BeginForm() you can specify an element by it's ID to update, and by returning a View you have greater control over what is returned in comparison to returning Content.

@using (Ajax.BeginForm("MyActionHome", "Home", new AjaxOptions {HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "resultArea"}))
{
    <input type="submit" name="Submit" value="Submit" /><
}

<div id="resultArea">
</div>   

This form allows you to specify the Action,Controller, Options and if you want additional arguments to send. We have also specified the TargetId that we want to update, in this case the 'resultArea'.

If you needed some clientside code to execute you could also use the OnComplete option and provide a JavaScript function.

[HttpPost]
public ActionResult PurchaseUnit()
{
    return View("_ResultPartial",);
}

Here we have a basic controller which returns a PartialView. That Partial will then be inserted into the Id specified, and will Replace, as defined by our options

JEV
  • 2,494
  • 4
  • 33
  • 47
1

If I understand you correctly you need to use Ajax.Beginform (or Ajax.Action). So, have a look on this answer I hope it will help you https://stackoverflow.com/a/5410121/2115584

Community
  • 1
  • 1
Baximilian
  • 885
  • 7
  • 25