I'm having trouble using unobtrusive ajax to post data from a contact us form.
My Partial View is like this:
@model site.ViewModel.Home.ContactUsViewModel
@using (Ajax.BeginForm("_ContactUsPartial", "Home",
new AjaxOptions { UpdateTargetId = "result" }))
{
<fieldset>
<legend>Contact Us</legend>
@Html.ValidationSummary(true)
<div id="result"></div>
<div class="editor-label">
@Html.LabelFor(m => m.FullName)
@Html.ValidationMessageFor(m => m.FullName)</div>
<div class="editor-field">@Html.TextBoxFor(m => m.FullName)</div>
<!-- other fields from model -->
<input type="submit" value="Submit"/>
</fieldset>
}
My Partial View is placed on another page like this:
@Html.Partial("_ContactUsPartial", new ContactUsViewModel());
My Home Controller like this:
[HttpPost]
public ActionResult _ContactUsPartial(ContactUsViewModel viewModel)
{
if (ModelState.IsValid)
{
try
{
//send email
return Content("Thanks for your message!");
}
catch (Exception ex)
{
return Content("Problem sending the email.");
}
}
return Content("oops. invalid model");
}
What I'm expecting to happen, is that when I press submit the controller will return some text content that will be placed in the div with an id of "result".
What is actually happening is when I press submit, I am getting redirected to /Home/_ContactUsPartial with the text content being the only thing that is displayed.
I don't understand what is going on here...and I have tried to look around at examples online only to find myself digging deeper into this hole.
I do have unobtrusive javascript enabled in my Web.config and I also have a reference to jquery.unobtrusive-ajax.min.js.
In my Scripts folder I have the following that may be relevant:
- jquery.unobtrusive-ajax.min.js
- jquery.validate.unobtrusive.min.js
- MicrosoftMvcAjax.js
In my _Layout.cshtml tag I have the following declared:
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/bootstrap.min.js")" type="text/javascript"></script>
In my Global.asax.cs Application_Start I have this line:
BundleConfig.RegisterBundles(BundleTable.Bundles);
RegisterBundles looks like this:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
"~/Scripts/jquery-ui-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));
Not sure what bundles do and how they work because I seem to have it configured to bring them in via this ScriptBundle...but then I also have them configured manually in the head of the _Layout.cshtml....
Any idea what I'm doing wrong? I'd really appreciate some help with this!
Microsoft.Unobtrusive
libraries have been removed from the project? I ran into this issue a few times and it ended up being library and dependency issues. – Turcotte