Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax.BeginForm partial view replace whole page

I had some problem in my MVC application, when I use button (submit) everything is okay but if I use onchange, partial view opened in the same window not replaced any div, I use all methods what I see here, but nothing help, here all code (any does not work correctly). Why its Happens?

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        List<string> Year = new List<string>();
        Year.Add("1");
        Year.Add("2");
        Year.Add("3");
        Year.Add("4");
        ViewBag.Years = new SelectList(Year);
        return View();
    }
 public PartialViewResult testt()
    {
        return PartialView();
    }
    [HttpPost]
public PartialViewResult testTwo()
        {
            return PartialView();
        }
}

View (I am include on my page all scripts unobtrusive ajax, microsoft ajax, jquery and many others):

<script src="~/Scripts/respond.min.js"></script>
<script src="~/Scripts/_references.js"></script>
<script src="~/Scripts/bootstrap.js"></script>
<script src="~/Scripts/jquery-1.10.2.intellisense.js"></script>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="~/Scripts/jquery.validate-vsdoc.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="~/Scripts/MicrosoftAjax.debug.js"></script>
<script src="~/Scripts/MicrosoftAjax.js"></script>
<script src="~/Scripts/MicrosoftMvcAjax.js"></script>
<script src="~/Scripts/MicrosoftMvcValidation.js"></script>
<script src="~/Scripts/modernizr-2.6.2.js"></script>
<script src="~/Scripts/respond.js"></script>
@using (Ajax.BeginForm("testt", new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "testDiv", InsertionMode = InsertionMode.Replace }))

{
<div>
    <div >
       @Html.DropDownList("UnitsNames", (SelectList)ViewBag.Years, new { onchange = "$(this.form).onsubmit()" })
    </div>
    <div >
        @Html.DropDownList("Years", (SelectList)ViewBag.Years, new { onchange = "$(this.form).submit()" })
    </div>
    <div>
        @Html.DropDownList("Years", (SelectList)ViewBag.Years, new { onchange = "this.form.submit()" })
    </div>
    <div>
               @Html.DropDownList("Years", (SelectList)ViewBag.Years, new { onchange = "this.form.onsubmit()" })
    </div>
</div>
}

@using (Ajax.BeginForm("testTwo", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "testDiv", InsertionMode = InsertionMode.Replace }))

{
<div>
    <div>
        @Html.DropDownList("Years", (SelectList)ViewBag.Years, new { onchange = "$(this.form).onsubmit()" })
    </div>
    <div>
        @Html.DropDownList("Years", (SelectList)ViewBag.Years, new { onchange = "$(this.form).submit()" })
    </div>
    <div>
        @Html.DropDownList("Years", (SelectList)ViewBag.Years, new { onchange = "this.form.submit()" })
    </div>
    <div>
        @Html.DropDownList("Years", (SelectList)ViewBag.Years, new { onchange = "this.form.onsubmit()" })
    </div>
</div>
}

<br>
<div id="testDiv">
    <h4>here i need a partial view</h4>
</div>

Thanks!

like image 421
user3615792 Avatar asked Nov 22 '25 08:11

user3615792


1 Answers

You have to include

@section scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

this in your page, else it won't work. Try like this, and not with the script tag.

like image 118
Strawberry Avatar answered Nov 25 '25 00:11

Strawberry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!