Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button click from partial view redirects to Parent action method

I have a View 'Parent' in my application , I am using @Html.BeginForm & a submit button to save data of Parent view . I also have one partial view inside Parent view , I am trying to call ajax method(using jQuery) from partial view but each time it redirects me to action method of parent form.

Parent View
@using (Html.BeginForm("Test1", "Home", new { id = 1 }, FormMethod.Get))
{
    @Html.Partial("TestPartial", Model)
    <div>
        <input id="Button1" type="submit" value="Parent" />
    </div>
}

Partial View
<script>
function onClientClick() {
    $.ajax({
        url: "/Home/Test2",
        type: "GET",
        dataType: "json",
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify({ ID: 1 }),
        cache: false,
        success: function (data) {
            alert('Success');
        },
        error: function (err, result) {
            alert("Error" + err.responseText);
        }
    });
}
</script>
<div>
<input type="submit" name="submit" onclick="onClientClick()" value="Submit" />
</div>

Each time i am trying to call /Home/Test2 from Partial view it redirects me to /Home/Test1(Action of parent form)

like image 947
nirav Avatar asked Mar 26 '13 11:03

nirav


People also ask

How to call partial view in HTML?

Rendering a Partial View You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .

What is the right way to render partial view?

You can render a partial view using Html. Action helper method. Like Html. RenderAction requires controller child action method which return PartialViewResult.

How to use partial view in mvc with example?

To create a partial view, right-click on view -> shared folder and select Add -> View option. In this way we can add a partial view. It is not mandatory to create a partial view in a shared folder but a partial view is mostly used as a reusable component, it is a good practice to put it in the "shared" folder.

How to use partial view in layout in mvc 5?

A partial view is a Razor markup file ( . cshtml ) without an @page directive that renders HTML output within another markup file's rendered output. The term partial view is used when developing either an MVC app, where markup files are called views, or a Razor Pages app, where markup files are called pages.


2 Answers

The problem is in this line:

<input type="submit" name="submit" onclick="onClientClick()" value="Submit" />

The type is submit therefore it is submitting the form and redirecting you to the Action in the parent.

In your partial view have a regular button not a submit button to request the AJAX content:

<input type="button" onclick="onClientClick()" value="Ajax Call" />

You could also return false however I believe using the standard button rather than submit is more suited in this context.

like image 180
Darren Avatar answered Nov 03 '22 07:11

Darren


Return false after calling the ajax... at the end of onClientClick function

OR Re-Write your submit as

<input type="submit" name="submit" id="submit" value="Submit" />

and then in jquery bnd the click event as

$("submit").click(function(e){
e.preventDefault()
$.ajax({
        url: "/Home/Test2",
        type: "GET",
.
.
. 

});

OR Change the input type from submit to button

like image 29
Raab Avatar answered Nov 03 '22 07:11

Raab