I use Ajax
call to create records in Create view and after creating a record I return id
and title
values to the success
of Ajax
method. I call another View called Completed in the success by sending the id and title values to it. However, I cannot use Url.Action
in the success
method because I cannot get response values in it. On the other hand, I do not want to use &
or ?
in the parameters when I use another way as mentioned below. So, how can I solve this problem?
Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Exclude = null)])
{
//some stuff
var redirectUrl = new UrlHelper(Request.RequestContext).Action("Completed", "Issue", new { /* no params */ });
return Json(new { success= true, url = redirectUrl, id= model.ID, title = model.Title, });
}
public ActionResult Completed()
{
return View(new { id = Request.Params[0].ToString(), title = Request.Params[1].ToString() });
}
View (Create):
$('form').submit(function (event) {
event.preventDefault();
$.ajax({
type: "POST",
url: '@Url.Action("Create", "Issue")',
data: formdata,
dataType: "json",
success: function (response) {
if (response.success) {
//Method I : Cannot use response values at here
window.location.href = '@Url.Action("Completed", "Issue", new { id = response.id, title = response.title })';
//Method II : I do not want to use ? as it is not recommended for security
window.location.href = response.url + "?id=" response.id.toString() + "&title=" + response.title;
}
}
});
});
View (Completed):
<div>
@(ViewContext.RouteData.Values["id"]) - @(ViewContext.RouteData.Values["title"]) has been created.
</div>