I am trying to use jQueryUI dialogs to show a list of matched data and return the chosen data, via JSon to the original view using ajax/jquery.
The flow is View (user completes textbox and clicks hyperlink) > Partial View in jQuery dialog > JSon data back to form.
My initial question is :-
Q. Should this be possible or am I trying to do something impossible?
If it should be working, here is my code
Index.view
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@Html.TextBox("Postcode") <a href="#" id = "PCSearch">Search</a>
<div id="mDialog"></div>
<script type="text/javascript">
$(document).ready(function () {
$("#mDialog").dialog({
modal: true,
width: 550,
height: 250,
resizable: true,
position: 'center',
title: 'Select Correspondent',
autoOpen: false,
dataType: 'json',
//open: function (event, ui) { $(".ui-dialog-titlebar-close").hide(); },
success: function (data) {
}
});
$('#PCSearch').click(function (e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "/Item/Search",
dataType: 'html',
data: { Postcode: $("#Postcode").val() },
success: function (data) {
$("#mDialog").html(data).dialog('open');
console.log("Hello");
}
});
});
});
</script>
ItemController
[HttpPost]
public ActionResult Search(string postcode)
{
if (postcode == null || postcode == "")
{
return PartialView("SearchByPostCode", null);
}
var model = Correspondents.Where(x => x.Postcode.ToLower().Contains(postcode.ToLower()));
return PartialView("SearchByPostCode", model);
}
[HttpPost]
public ActionResult ChooseCorrespondent(int CorrespondentID)
{
return Json(CorrespondentID, "text/html");
}
The flow is working fine, User enters text, the Item/Search PartialView is displayed in a modal dialog, when the user selects a button the ChooseCorrespondent is hit, but the page redirects to a blank screen with the CorrespondentID, rather than back to the calling page.
I have tried a number of examples and methods to catch the JSON and update the Index view, but it either fails(errors) or returns the Json to a blank page.
Q. What is the best method for catching the Json returns in these circumstances?
Thanks for taking the time to read this far!
Update
I have combined the javascript into:
$(document).ready(function () {
$('#PCSearch').click(function (e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "/Item/Search",
dataType: 'html',
data: { Postcode: $("#Postcode").val() },
success: function (data) {
$("#mDialog").html(data).dialog({
modal: true,
width: 550,
height: 250,
resizable: true,
position: 'center',
title: 'Select Correspondent',
autoOpen: false
}).dialog('open');
//How can I trap the Json returned from this dialog open?
//Using a bindForm function ?
}
});
});
});
I am trying to trap the returned Json and update the originating page.