0

I have a very simple view that has a DropDownListFor and a Button inside an Ajax.BeginForm helper. Clicking the button renders the whole view again inside the div I have set to update including the layout page (I also notice a spike in the cpu when clicking the button multiple times)

Here is the Ajax.BeginForm inside the view:

@using (Ajax.BeginForm("About2", "Home", new AjaxOptions { UpdateTargetId = "property22" }))
{
    <div>
        <div id="property22">
            @Html.DropDownListFor(m => m.SomePropertyToBind, new SelectList(Model.list, "property1", "property2"))
        </div>
        <button type="submit" id="test">
            Click me</button>
    </div>
}

Any ideas where I'm going wrong?

I uploaded the whole project if someone has a couple of minutes to take a look at it: http://www.sendspace.com/file/siu3r31 (free provider so there may be a popup)

Thanks

4 Answers 4

1

You are using a wrong overload of the Ajax.BeginForm helper. It should be like this:

@using (Ajax.BeginForm(
    "About2", 
    "Home", 
    null, 
    new AjaxOptions { UpdateTargetId = "property22" }, 
    new { @id = "refreshe" }
))

Notice the additional null value I am passing as the routeValues parameter. Also in the example you uploaded you forgot to include the TestView.cshtml view. This being said in order to fix the problem you have two possibilities:

Either return a partial view:

public ActionResult About2()
{
    Random randomizer = new Random();
    int random = randomizer.Next(1, 1000000000);
    ModelTest newModelTest = new ModelTest();
    string[] stringList = new string[] { "Red", "Blue", "Green" };
    newModelTest.list = from test in stringList
                        select new ModelTestList
                        {
                            property1 = test,
                            property2 = test
                        };
    newModelTest.SomePropertyToBind = stringList[random % 2];
    return PartialView("TestView", newModelTest);
}

or disable the layout in the TestView.cshtml view:

@{
    Layout = null;
}
0
0

Unfortunately from your explanation above and from the code, I am not sure what you are trying to achieve. However, I think your most worry is about having Ajax working in your view.

In your About2 action method, you are trying to return a complete view which is TestView (in that case, it doesnt exist) and passing it the newModelTest view Model. I would advise changing to return either a PartialView or JsonResult.

For example, changing the return statement of About2 action method to

    public ActionResult About2()
    {
        ...
        return Json(newModelTest);
    }

or changing it to a return type to string and returning "TestResult"

    public String About2()
    {
        ...
        return "TestResult";
    }

or you could change the return statement to return a PartialView

0

Thanks for your replies.

I just realized that About2 should have returned the "About" view instead of the "TestView". I had tried creating a partial view with the Ajax.BeginForm code but I came across the same problem.

This is my first attempt at Ajax.BeginForm (so far I have always used jquery), and I was under the impression that it works in a similar fashion in the sense that by specifying the target id only the contents of that element will get updated, not that the target will actually get replaced by the whole response object.

Thanks for your help, not only did I get it to work, but I now understand how it should work.

-1

I suspect that what's happening is that you're returning the a complete View (including the layout template) in the Ajax response. Try changing your "Home" controller "About2" action temporarily to the following:

public ContentResult About2() {
    return Content("Hello World");
}

I tested this sample Action with your Razor markup and clicking the button properly replaced your dropdown list with "Hello World!".

If this is indeed what's happening, then you'll want to return a View from "About2" without the layout by declaring the following at the top of the View that you're returning.

@{
    Layout = null;
}
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.