1

View:

@{
    AjaxOptions ajax = new AjaxOptions() { HttpMethod = "POST", UpdateTargetId = "sub_id" };
    Layout = null;       
}

<div id="sub_id"></div>

@using (Ajax.BeginForm(ajax))
{
    @Html.TextBox("email");
    <input type="submit" value="подписаться" />
}

controller:

[HttpPost]
public ContentResult LeftMenuSubscription(string email)
{
    return new ContentResult(){Content = "<script>alert('Thanks')</script>"};
}

"Thanks" alert show.

but in div sub_id set all page(<title></title><div>...</div>).

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script> 

- connected.

html:

<html>
    <head>
        <title>Главная страница</title>
        <link href="/Content/Site.css" rel="stylesheet" type="text/css">
        <script src="/Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
        <script src="/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"</script>
        <script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
        <script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
        <script src="/Scripts/jQueryFixes.js" type="text/javascript"></script>
    </head>
    <body>
        ....
        <form action="/" data-ajax="true" data-ajax-method="POST" data-ajax-mode="replace" data-ajax-update="#sub_id" id="form0" method="post">
            <input id="email" name="email" type="text" value="">
            <input type="submit" value="подписаться">
        </form>
        ....
    </body>
</html>

What could be the problem?

2
  • "but in div 'sub_id' set all page(...)." what do you mean by this? What's the error/issue you're having? Commented May 23, 2012 at 11:23
  • IN "sub_id" should get <script>alert('Thanks')</script> Commented May 23, 2012 at 11:45

1 Answer 1

1

Looks to me like your Ajax options aren't complete. I believe you need to add the Action that you are requesting from:

@using (Ajax.BeginForm("ActionName", "ControllerName", ajax))
{
    @Html.TextBox("email");
    <input type="submit" value="подписаться" />
}

Running this in a test I get a popup alert, which is exactly what should happen because you are returning a script that says: <script>alert('Thanks')</script>

If you just want the div to show Thanks then don't return the script tag

Use this if you just want to have the word thanks show up in the div

[HttpPost]
public ContentResult Test(string email) 
    return new ContentResult() { Content = "<span>Thanks</span>" };
}

This works perfect for me using the modified form

@using (Ajax.BeginForm("Test", "Test", ajax))
{
    @Html.TextBox("email");
    <input type="submit" value="Save" />
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.