asp.net mvc json open dialog box problem - asp.net

function postForm()
{
$.ajax({
type: "POST",
data: $("#myForm").serialize(),
dataType: "json",
url: '<%= Url.Action("JSONRequest","Home") %>',
success: function(result)
{
window.alert(result.name);
},
error : function()
{
window.alert('error');
}
});
}
Html.BeginForm("JSONRequest", "Home", FormMethod.Post, new { id = "myForm" });
Html.TextBox("mazhar")
<input type="submit" onclick="postForm" />
Html.EndForm();
public ActionResult JSONRequest(FormCollection form)
{
string a = form["mazhar"];
var data = new { name = "aaaa", Success = "Record is Succesfully Saved", ErrorMessages = "abc" };
return Json(data);
}
Ok the problem is that the dialog box is opening after running this code which is asking to save file.
Can someone tell me how to resolve this issue? Why does this box comes afterall?

You need to cancel the default form submission by returning false inside the button onclick handler:
<input type="submit" onclick="postForm(); return false;" />
That being said, I would suggest you a better solution. Use the jquery.form plugin which enables you to ajaxify an HTML form. This way much of the duplication in your code could be simplified:
Html.BeginForm("JSONRequest", "Home", FormMethod.Post, new { id = "myForm" });
Html.TextBox("mazhar")
<input type="submit" value="OK" />
Html.EndForm();
And in javascript:
$(function() {
$('#myForm').ajaxForm({
success: function(result) {
window.alert(result.name);
},
error : function() {
window.alert('error');
}
});
});
This way you no longer need to specify url, method, manually serialize form fields, etc... You also don't need to pollute your HTML markup with javascript functions. This is unobtrusive javascript. Another advantage of this approach is that now you will be able to externalize this javascript into a separate static .js file as it no longer relies on server side code (<%= Url.Action("JSONRequest","Home") %>) and this you will benefit from reducing the bandwidth and caching static resources.

I think you are posting the form twice. You should use Ajax.BeginForm instead of normal form. And remove the jQuery Ajax call.
Here is a very good example of using Ajax Form.
http://davidhayden.com/blog/dave/archive/2009/05/19/ASPNETMVCAjaxBeginForm.aspx
Or you can also try by replacing
<input type="submit" onclick="postForm" />
with
<input type="button" onclick="postForm" />

Related

Calling method from razor page

I have a asp.net core mvc project.
In my layout file, I want to display the name of the currently logged in user, such that the username is displayed in the header. For this, I want to be able to call a function in my homecontroller that does this.
So, I made a simple function taht looks like this in the home controller:
public String GetLoggedInuser()
{
return "garse garsebro";
}
And then I have tried every method I have been able to find. The first couple of methods here are just function suggested around the web, that are simply not available to me:
#HtmlHelper.Action("GetLoggedInuser");
#Html.RenderAction("GetLoggedInuser");
To name a few. Then there is this one, which I can find:
#Html.ActionLink("GetLoggedInuser")
But for this one, my function "GetLoggedInuser" can't be found anywhere.
How do you, in a razor page call a controller function that you can get returned a string from that function and display it?
If you are using Microsoft.AspNet.Identity then below line will do the work post login.
#Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
You can try to use ajax to call action to get the username,and add it to html:
<div id="username">
</div>
#section scripts
{
<script>
$(function () {
$.ajax({
type: "GET",
url: 'GetLoggedInuser',
}).done(function (result) {
$("#username").html(result);
});
})
</script>
}

Ajax.BeginForm is not working as expected

I have a very strange problem with Aajx.BeginForm. I have this code :
In view :
#using (Ajax.BeginForm("Upload", "Profile", new AjaxOptions() { HttpMethod = "POST" }, new { enctype = "multipart/form-data" }))
{
#Html.AntiForgeryToken()
<input type="file" name="files"><br>
<input type="submit" value="Upload File to Server">
}
In controller :
[HttpPost]
[ValidateAntiForgeryToken]
public void Upload(IEnumerable<HttpPostedFileBase> files)
{
if (files != null)
{
foreach (var file in files)
{
// Verify that the user selected a file
if (file != null && file.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(file.FileName);
// TODO: need to define destination
var path = Path.Combine(Server.MapPath("~/Upload"), fileName);
file.SaveAs(path);
}
}
}
}
The problem is that I get null file when the form is submit. I read many question that is the same of my question, but most of the answers was that the name of input type="file" is not as the same name of the parameter name in the controller. I found some examples, I try this one which is almost the same of my code except for the jquery files, so I tried to replace the jquery files with these files :
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
And here is the surprise !!. When the form is submit, I get the file but the form is post back. It is work as there is no ajax. I search in google for Ajax.BeginFrom that is post back and found many solutions in stackoverflow and most of of the answers was that jquery.unobtrusive-ajax file must be included in the page. It like a circle of problems, once you solve one you get another. Does I miss something ?
You cannot submit files with Ajax.BeginForm(). The helper uses the jquery.unobtrusive-ajax.js file to submit the data using ajax functions which do not allow multipart/form-data enctype.
One option is to use FormData (but not supported in older browsers). Change the Ajax.BeginForm() to Html.BeginForm() and then handle the forms submit event
$('form').submit(function() {
var formdata = new FormData($('form').get(0));
$.ajax({
url: '#Url.Action("YourActionName", "YourControllerName")',
type: 'POST',
data: formdata,
processData: false,
contentType: false,
success: function() {
.... // do something?
}
});
});
In addition there are numerous jquery plugins that you can use for uploading files (14 of them are listed here)
Side note: Your file input allows selection of only one file, so your method parameter should be HttpPostedFileBase files (not IEnumerable<HttpPostedFileBase> files) or alternatively, include the multiple attribute in the file input.
You need to specify the encoding type in your form.
#using (Ajax.BeginForm("Upload", "ControllerName", new AjaxOptions { HttpMethod = "POST"}, new { enctype = "multipart/form-data"}))
{
#Html.AntiForgeryToken()
<input type="file" name="files"><br>
<input type="submit" value="Upload File to Server">
}

Need to update the database without navigating to another page in classic asp

I have a classic asp page that fetches data from a table. I have a button to update some values in the database. This is the code I have used.
Code in SelectData.asp
<p>
<input type="button" value="Reset" onclick="location.href='RecordsUpdate.asp'"></p>
When I click the button it redirects to RecordsUpdate.asp page and updates the database (RecordsUpdate.asp has the code to update the database and it's working fine). I need to update the database but I don't want to navigate to RecordsUpdate.asp page. I need to stay on SelectData.asp page itself, but when I click the button the data should be updated in the database. How can I do that?
Please help me out.
Thanks In Advance,
Abhilash D K
You'd be best off using jQuery for this so you can capture the button 'onclick' and use Ajax to "get" the RecordsUpdate.asp page:
<input id="mybutton" type="button" value="Reset">
<script>
$("#mybutton").on("click", function(e) {
e.preventDefault();
$.get( "RecordsUpdate.asp", function( data ) {
//data will be the output from `RecordsUpdate.asp`
alert( "Load was performed." );
});
});
</script>
I am able to call an .asp page without navigating to it. This the code that I used.
$(document).ready(function () {
$("#updatebutton").on("click", function (e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "RecordsUpdate.asp"
})
.done(function (msg) {
alert("Data Saved");
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Thank You all for helping me out.
Abhilash D K
You need to do this with javascript. Look here for more info: http://www.w3schools.com/ajax/ajax_xmlhttprequest_create.asp
If you are using jQuery, use this http://api.jquery.com/jquery.ajax/
With the success callback you can retrieve some responsedata so you can notify the user if you want. This ain't rocket science.

In ASP.NET, a POST/Redirect/GET sequence with AJAX hits the redirected-to action twice

Inside a view, I have the following:
#using (Html.BeginForm())
{
<input type="submit" id="savebtn" value="Save" onclick="saveLayout()"/>
}
<script type="text/javascript">
function saveLayout() {
$.ajax({
url: '/Page/SaveFaces/',
data: {
/* layout data of the page, irrelevant */
},
type: 'post',
success: function () {
}
});
return false;
}
</script>
The above hits the following action, which simply redirects the user back to the URL they came from (it's also supposed to save the data, but I've removed that part for simplicity, as it doesn't affect the problem):
[HttpPost]
public ActionResult SaveFaces(string items)
{
return Redirect(Request.UrlReferrer.AbsoluteUri);
}
Then, due to the redirect, we go back to this pretty standard model-fetching action:
public ActionResult Index(int id = 0)
{
var page = db.Pages.Find(id);
if (page == null) return HttpNotFound();
return View(page);
}
The problem is that this last action is called twice.
I have tried removing the AJAX call and doing a normal POST operation and the problem goes away. However, the data I'm trying to send is obtainable only through the a jQuery script and I can't put them in a form. I'm constrained to work with the AJAX method.
Is there anything I can do to prevent the action from being hit twice?
I see you are using jQuery. Can you try this instead? (Note you may have to bind the the form submit event rather than the input button, or both)
#using (Html.BeginForm())
{
<input type="submit" id="savebtn" value="Save" >
}
<script type="text/javascript">
$("#savebtn").submit(function saveLayout(event) {
// The magic that prevents post.
event.preventDefault();
$.ajax({
url: '/Page/SaveFaces/',
data: {
/* layout data of the page, irrelevant */
},
type: 'post',
success: function () {
}
});
return false;
}
</script>
Also if you have access to form element, another way:
<form onsubmit="javascript: return false;">
Though it might be a bit specific to my scenario, I just found an acceptable solution. Since all the data is provided through jQuery, I removed the form completely and replaced the submit button with a simple link.
So, this goes away:
#using (Html.BeginForm())
{
<input type="submit" id="savebtn" value="Save" />
}
And this is put in place instead:
<a onclick="saveLayout()" id="saveLink">Click to save.</a>
Now the [HttpPost] action is hit, the data is saved and the redirected-to action is also hit, once.

how to do I redirect from form actions using JQuery UI Dialog?

I'm using a Rails url helper that (for bot protection, etc.) nests redirection inside a form, like so:
<form method="post" action="/projects/1/personal/4/edit_income?criterion=4" class="button-to"><div><input name="_method" type="hidden" value="put" /><input class="requires_confirmation" type="submit" value="edit space parameters" /><input name="authenticity_token" type="hidden" value="4Q/E359jxXUcu7TSVm+cqdjj94RAdQpV+DIv7OUQ+Gg=" /></div></form>
(Above, the method doesn't have to be a POST...)
I want to handle clicks on this .button-to element with jQuery UI Dialog, like so:
$('.button-to').click(function(e){
$( "#dialog-confirm" ).dialog({
resizable: false,
height:250,
buttons: {
"Save": function() {
// SAVE CERTAIN DATA ELEMENTS HERE...
$( this ).dialog( "close" );
// FOLLOW FORM REDIRECT...
},
"Don't save": function() {
$( this ).dialog( "close" );
// FOLLOW FORM REDIRECT...
},
Cancel: function() {
$( this ).dialog( "close" );
// BUT CANCEL REDIRECT IN THIS CASE!
}
}
});
})
It seems like this kind of thing would come up all the time, but I can find nothing helping me toward the result I seek. Any suggestions?
Thanks,
Lille
Came across this question after posting a similar one. Basically, I found using the .data collection works best. Set some vars in $('#dialog').data before instantiation and then you can use the form to modify them as needed.
Then you can use the public methods of the dialog (or your own) to examine those values and act accordingly.
For more in see my question and my response to myself for the similar question.

Resources