I have to call post action method in controller. my razor view is
#using (Html.BeginForm())
{
#Html.TextBox("count")
// here i have many controls
<input type="submit" value="SUBMIT"/>
}
[HttpPost]
public ActionResult Update(string count)
{
// i will do many business related actions
return View();
}
My question is, can i use normal post call by clicking submit button or shall i use ajax post method?
Which method of calling is good in mvc3 and why?
You must know the difference between Normal post and ajax
if you dont want to refresh your page then use ajax i.e you have to update content dynamically and stay on that page , otherwise use normal post
Both methods are fully supported, and the choice depends on your scenario.
You may use Ajax post method if you don't want to reload all your page on submit. Ajax allows you to make some partial refresh, so you refresh only a part of your page WHILE the normal post call loads an entire page(including headers and some other content that may not be changing).
garethb has given an answer in this link http://www.codeproject.com/Articles/429164/Html-BeginForm-vs-Ajax-BeginForm-in-MVC3.
the answer is
Ajax forms are suitable in situations, where you need to do modify or save operations asynchronously , without redirecting to any other forms.
You are using the MVC of ASP.NET, which is server-side. Posts are issued by the Javascript written on the client-side. Posts are supported by your server-side, so you can use "normal" or ajax posts as you like.
Related
When we click a Form's submit button, then action of the controller which is having HTTPPost attribute is called but what if i want to call or perform an action when a normal HTML button is clicked
Although following articles
http://www.codeproject.com/Tips/198477/Calling-a-MVC-Controller-and-Action-Method-using-H
HTML button calling an MVC Controller and Action method
tells the approach but both of these are using controller name in view, So view has to know about controller, I am looking for an answer that view wont have to know about controller.
because views has to be Independent from Controller, Views should not know about controller
So, if you know the answer then please reply
any form that directs your user to url created by
<a href='#Url.Action("{action}", "{controller}")'> click me </a>
or
#using(BeginForm("{action}", "{controller}")
will do what you want.
That can be with a
form
button link
It's the destination that matters. The View does not "know" anything about the action or controller. The helper does.
To execute an MVC action from the client side (i.e. from a view) you need to hit a URL (with any verb: get, post, put, etc).
Therefore to execute an action form a view you will need to know the URL of that action, by default that URL is directly mapped to the controllername/actionname but you can re-define this if you want to create more abstraction between view and controller.
Given this your button just needs to be a link to a URL or linked to js to do an Ajax http request.
Hope that helps.
You cannot have 2 actions on the same controller with the same name and the same HTTP verb. So what you are asking doesn't make sense. You could invoke the same controller action as the one that rendered the view without specifying an action and controller name. The reason why Html.BeginForm() works without specifying an action and controller name is because the form is sending a POST request to the server and you can distinguish the 2 actions.
I am newbie with asp.net MVC and i want something simple. I have an Index.aspx page and a UrlContent.cs class.
I am searching how to write the code of the button_click listener of the page in the class. So far havent found anything on google.
Thats all, thank you
MVC is a different paradigm, and doesn't really have the concept of "event listeners".
That concept was always an abstraction from how web clients/servers really communicated. To a web server, there's really only one event, and that is an HTTP request from the client. To achieve the illusion of "events", ASP.Net does some (Javascript+cookies) magic behind the scenes, and creates hidden form input tags -- containing info about which button was clicked -- within a standard HTML form, and posting the form back to the server.
MVC adheres much more closely to the native behavior of HTML/HTTP. It requires you to get accustomed to working with those technologies -- forms, GET/POST requests, and AJAX.
To handle a (submit form) button click event, you create an action in your controller that accepts parameters.
Controller
[HttpPost]
public ActionResult Index(MyModel model)
{
// handle the submit button's "click event" here
}
View
#model MyModel
#using (#Html.BeginForm("Index", "Home")) {
#Html.EditorForModel
<input type='submit' value='submit' />
})
If you're running MVC, I think you're looking for something like this
#Html.ActionLink("Link Text", "Action method Name", "Controller name",new {} , new {})
Here's some documentation on more overloads.
So I have a scenario where I want to return my ActionResult...Return View("ViewName", "MasterPageName",model); in a popup window of a specific size I can pass...
E.G.:
public ActionResult PopUp()
{
//do some work...
//I want this returned in a popup window/modal dialog of some sort...
return View("ViewName","MasterPageName",model);
}
what is a reasonable way to accomplish this from the controller in asp.net mvc?
thanks in advance.
Nothing can be done on server side but you can decorate your action links like
<%= Html.ActionLink("Pop Up", "PopUp", null, new {target="_blank"}) %>
You can't manipulate the client side browser from a Controller on the server-side. What you can do is output script in your returned view, or call a controller that returns data via an AJAX call and pop-up from the client-side.
This is not something you can really do from your controller as this is code that executes on the server as the result of a http request and returns a response of some form or other. You will need to do this on the client, probably using javascript or alternatively you can call your controller action and specify the target attribute of the a tag as '_blank'.
Maybe you can try dynamically loading the rendered view using jQuery.load()
I've used jQuery dozens of times with PHP with great success. I'm working on an ASP.NET application and would like to use jQuery in the same manner.
Basically, I've got a masterpage that has the form and a webform that has all the form fields and data. A user can submit the form multiple ways - selection of a drop-down, button, etc. I want to catch all submits and use jQuery to submit the form. While the form is being processed, I want to display a new DIV with some text in it. Finally, I want to replace that div with the new form.
How can I accomplish this with the way that ASP.NET works?
Actually ASP.NET will post-back if you use its built-in JavaScript __doPostBack function. There's no other painless way for doing that.
That means you can use jQuery to handle drop-down lists, buttons or whatever (X)HTML element event and handler's body will invoke __doPostBack.
It's unclear that you want is a full-postback, but a partial one using AJAX.
If you're looking for a solution for sending form values to the server without a full-postback, I believe you've these options:
Callback API: http://msdn.microsoft.com/en-us/library/ms178208.aspx
Page methods, update panels: http://msdn.microsoft.com/en-us/magazine/cc163480.aspx
Anyway, let me give you an advise: ASP.NET works quite different compared to PHP and you'd not try to reproduce some known PHP solutions in ASP.NET. You need to change your mind.
About showing a DIV or anything while something is processed, play with initializeRequest ASP.NET AJAX PageRequestManager:
http://msdn.microsoft.com/en-us/library/bb397460.aspx
But that would depend on what AJAX API you're using, because since Microsoft AJAX will be replaced by jQuery in the next times, I'll need to say that you need to do that in some jQuery approach, like creating some $.ajax wrapper so your code will be able to listen when an asynchronous request is going to be made and you can perform actions by handling that situation like showing a DIV or any loading notice.
In ASP.NET Webforms formposts aren't as easy as they are in php. If you're new in ASP.NET development try http://www.asp.net/mvc. A common framework which allows you to implement TypedViews (ViewModes), simple request to modelbinding, and so on...
mh, sample:
[HttpPost]
public JsonResult Insert(string name, string vorname) // name&vorname filled by $_POST:)
{
var #new = new Person { Name = name, Vorname = vorname }
this.repo.Insert(#new);
return this.Json(new { success = true, newId = #new.Id });
}
I have an "Edit Profile" lightbox on my page, which posts to the controller via jQuery ajax. I handle the response in the jquery end instead of returning a View. However, when the profile is saved, I need to refresh the values on the page displaying the popup. How could I achieve this in MVC2? For example, if the user changes her name and avatar (in the lightbox), after she saves the profile, I'd like to update the avatar and name everywhere it occurs on the page.
Well what i would be doing is make your Controller return a PartialViewResult, which the end result is basically HTML.
The Partial View would be the popup itself, so essentially your calling your Controller method via AJAX, doing your server-side work, then re-rendering the Partial View to the client.
Have the action you post to via jQuery return a success for failure message. If it is a success, change the avatar/name/etc on the page using the values already in the textboxes (i.e.: the values you posted to the controller). If it is a failure message, display the validation errors.
In your jQuery AJAX, everything can be done in the callback function of the AJAX request.
Prabhu - both your profile page (i.e. the 'main' div contained within it) and the popup div should be partialviews. on posting the popup back to the server, you should requery the main page partialview and return the appropriate html, targetting the 'main' div.
that's certainly the approach that i take for a very similar task.