I am calling controller action method from dialog box from view. I want to return to same dialog box in same view. what return type should I specify in controller action method.
I suggest you take a look at Progressive enhancement tutorial with ASP.NET MVC 3 and jQuery. It provides a pretty simple example on how to update a dialog box with new content by adding jQuery to perform the requests needed for updating the UI.
Hope it helps!
Related
I have a page with for example personal data, I want to have a Go button for other items such as job data but on the same current page with a Go button (next) and so on...
What is this characteristic called Please provide a link to it, or a brief explanation. (like multiview in old version 'asp.net web form')
I'm using asp.net core
This problem could be solve by using ajax.
In your controller you have to make a action just like this
public JsonResult GetYourData()
{
//Add your data here. Make a string along with your data like '<h2>Job Data</h2><p>Title:Software-Developer</p>'
return Json(yourCreatedString, JsonRequestBehavior.AllowGet);
}
Now in view you could add an event on the button click, ask for this above action, get the html in string format, and append that to your DOM
Working on MVC5 web app.
I have a view based on a complex viewmodel. In the main GET method I'm filling this viewmodel, setting certain ViewBag items, etc.... I'll refer to this as the "load code" for now.
So, my view is basically a data entry form. Business rules are fairly complex. When the user hits the save button I do a POST and run some c# code which checks rules, etc... If it FAILS, I simply want to "abort" the action and show the user the same GET view.
My question involves all that was done to initially load the view: The ViewBag code, filling of viewmodel, etc.... Do I have to write this "load code" again in my POST? This seems very redundant. Is there a way I can just "abort" the ActionResult/POST and show the GET view?
I hope this makes sense. Thanks!
**** update/clarification - based on comments ****
I suppose this question is more of a "general" one. Can you "abort" the POST and go "back" to the GET View without re-running all the "load code" (for the dropdowns, populating the viewmodel, etc....)
If you want to go to server and validate and then decide if you want to accept the POST or Fails/Abort and keep the previous page with all the values
then I'll suggest you to handle this in AJAX.
Create a third action/API Method and keep your validation logic there. then on the form submit hit the validation action using ajax and server will return True/False (with any error message) if you get True from server then let the javascript post your form otherwise show any error message and return false to stop posting the form and stay on the page.
For example use a javascript method to validate.
<form onsubmit="return isValidForm()" />
function
function isValidForm(){
//// hit the server and see if you want to proceed with the POST
//// if fails return false so your form will not be posted and all of your values will remains the same, here before return false you can clear the form if needed.
}
let me know if you need more information.
I have a HomeController with an action About. I want to achieve this behavior:
Case #1 User open the page /home/about and the view render fully (return View();)
Case #2 User is on the homepage and clicks a link about, I want to change the url using history.pushState and render it partially (return PartialView();)
How can I achieve this behavior?
You can use Request.IsAjaxRequest to detect whether the controller action was invoked using an Ajax request.
As an alternative I'd recommend splitting your views so into smaller pieces, such that your About.aspx view uses the partial view, e.g. by doing Html.RenderPartial( "AboutBox" ). This gives you the power of reuse without having to clutter your actions with if-sentences.
yet another.
Restrict ASP.NET MVC Action's Using The ActionMethodSelectorAttribute
select action method control at ActionMethodSelectorAttribute.
My view gets rendered in an alert window. I have a post action that adds a new record to my repository, and then returns a list of matching objects for display:
[HttpPost]
public ActionResult Add(FormCollection collection)
{
...
_repository.AddMyObject(myobject);
_repository.Save()
_matchingResults = _repository.GetMatchingResults(myobject);
if (Request.IsAjaxRequest())
return View("Results", _matchingResults );
...
}
"Results" is a view that renders a list of matchingResults. However, all I get is an alert window with the rendered html. I can't use RedirectToAction because I need to pass in _matchingResults.
Any suggestions?
Your view rendering the results should be a partial view i.e. Results.ascx (user control) and then you would return that to the view via return PartialView("Results", _matchingResults)
One work around although ugly might be to use Tempdata to store what you want and retrieve it in your Results action as TempData persists between two requests
However TempData can only store strings but Phil Haacked comes to a rescue with this blog.
A common pattern when submitting a
form in ASP.NET MVC is to post the
form data to an action which performs
some operation and then redirects to
another action afterwards. The only
problem is, the form data is not
repopulated automatically after a
redirect. Let's look at remedying
that, shall we?
I have a contoller action that a number of forms will post to, all in different views.
Is there a way, in my controller action, to see which view contained the form that posted to it?
I need this to determine to where to redirect the action when the code in the post action is complete.
Thank you!
Two options...you can add a field to your form (or query string) that provides the redirect url. Or you can look at the HttpRequest.UrlReferrer field. It will provide you with the full URL which you have to parse to get the original form.
Hope this helps