Show result in the same view after sending MVC

I have a form with two fields that pass values โ€‹โ€‹to a stored procedure. The stored procedure will return 0 or 1.

If 0, the user does not have the right to view the data. If 1, it is. Then I want to show the details on the same page that I submitted. I am using an MVC 4 razor.

Please give some idea of โ€‹โ€‹how to achieve this. I am new to MVC.

+4
source share
3 answers

You can use javascript ajax to post your post. 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>
+5
source

If you want, you can use Ajax Helper MVCs and use Ajax.BeginForm () or use javascript and the standard post form. Whatever the choice, in action just return the idea.

If you use Ajax.BeginForm (), you can specify the element by its identifier for updating, and returning View will give you more control over what is returned compared 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>   

Action, Controller, Options , . TargetId, , "resultArea".

- , OnComplete JavaScript.

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

, PartialView. Partial Id ,

+3

, Ajax.Beginform( Ajax.Action). , , , fooobar.com/questions/26748/...

0

All Articles