I’m new to MVC. I want to be able to change a button/divs/textboxes text when a view’s button is clicked. I found a tutorial online that showed me the following but when I click the button I’m getting redirected to another page. That page shows the text instead. I haven’t touched the default Global.aspx
View
@using (Ajax.BeginForm("ExamineTextBox", new AjaxOptions { UpdateTargetId = "result" }))
{
@Html.TextBox("textBox1")
<input type="submit" value="Button" />
<span id="result" />
}
Controller
public string ExamineTextBox(string textBox1)
{
if (textBox1 != "Initial Data")
{
return "This text is MVC different from before!";
}
return String.Empty;
}
Make sure you have included the
jquery.unobtrusive-ajax.jsscript to your page.If you are using the default bundles (take a look at
~/App_Start/BundleConfig.cs– you will see ajqueryvalbundle defined which combines and minifies all~/Scripts/jquery.unobtrusive*and"~/Scripts/jquery.validate*"files):and if not using bundles you could include only this script individually:
It is this script that is required for
Ajax.*helpers to work. As it name indicates it unobtrusively AJAXifies them. It depends on jQuery so make sure you have that one included as well.Side note: In ASP.NET controller actions should return ActionResults, not strings:
Here’s how your full view code might look:
and the controller: