how can i show an popup coming from a controller after my task has finished? something like to notice the user it has been finished?
You have to return an ActionResult from the controller action that can be rendered by the browser. That's usually either a whole page, or a partial page or JSON data returned to an Ajax call. The javascript in the page can then display the result returned.
I resolved this as following:
step 1- create a partial view name "_Empty.cshtml"
then, in that view you can send message to end user based on your controller condition.
and in the controller write this two lines:
ViewData["AlertMessage"] = AlertMessage.NoClient;
return PartialView("_Empty");
here the user not select any client, so i redirect user to empty partial view and tell user that please select client then proceed to another opration.
Related
This is about an MVC WebApp (that also uses EF). My question is about returning to the same view after an asp-action.
I display a list of users (db records on my .cshtml page). Next to each user, I have a Reset Password hyperlink. Upon clicking the hyperlink, I use EF to reset the user's password. This is done through an asp-action
<a asp-action="ResetPassword" asp-route-userid="#item.userid">Reset Password</a>
This reset asp-action is all happening fine.
But post asp-action, I would like to return back to the same view (from where I originally clicked the Reset Password hyperlink).
I do not need any refreshes or re-launch of the view. I find examples in StackOverflow about using Ajax if its a form/submit button action. In my case there's no submit button action or form or another view being shown.
Any suggestions on how to return back to the same View?
In your controller ResetPassword method, you can redirect to the action that initially displayed your view. You don't have your controller or view names listed in your question, so I'll make some guesses here:
public class UsersController : Controller
{
public ActionResult Index()
{
//get user data/setup model (or do it from the view via ajax)
return View(model);
}
public ActionResult ResetPassword(int id)
{
//do reset password work
//return redirect to the Index page that lists your user data
return RedirectToAction("Index", "Users");
}
}
Now, while the above will work, it would be a better experience for the user (and lower server/db impact) if the call to ResetPassword was done via ajax, since a full page refresh wouldn't be needed.
In a standard MVC application we have _Layout.cshtml and an Index.cshtml view. Now imagine if the user receives an email activation after a registration and now he/she clicks on that link.
The link points to /Account/ActivateAccount and after I process the activation, I wanted to redirect the user to the Login.cshtml partial view displaying a message.
I'm aware I can pass the message via the TempData["Message"] but what I don't know is how to redirect to a partial view and have that display inside the _Layout.cshtml instead that by itself.
At least this is what is happening when I call RedirectToAction("Login", "Home")
Note: all "home" based partial views are displaying within a < div id="divmain"> and my ideal solution would be to be able to decide what view should display inside that div, from the controller.
What's the right way to call a Partial View to be displayed from within another Controller ??
I'm not entirely sure I understand the question but I think what you're asking is if you can call a partial view from the _layout? This can be done with #Html.Action() helper. It will call out to a controller and method you specify and insert the result. The method you call would just be a PartialResult that returns the partial view with whatever data you need.
This is in contrast to #Html.Partial() which will render the partial view in place with whatever data you provide without routing back through a controller.
EDIT:
To summarize the comments to this answer, it seems I misunderstood the requirement. A user receives an email and clicks the link to activate their registration. The controller action that handles that request activates the user and then it needs to redirect to something. In this case, there was already a partial view which would serve the purpose but it couldn't be redirected to directly for some reasons. My suggestion was to just create a new View which contained a #Html.Partial() call to basically wrap the partial in a full view page.
The action method which handled the click could then just return that view directly or, if you consider the click of the link to be a "post" since it changes the application model by validating the user, you would create a new controller action and return RedirectToAction() directly. This would be like following the Post, Redirect, Get method and would prevent some issue if the user tried to refresh the "activated" page. It would also give more control over the URL naming.
Imagine the ForgotPassword sent an email with a link to Recover the password. Ideally we want the RecoverPassword to be a PartialView and it has to run inside the HomePage itself.
The external link passes a GUID.
QUESTIONS:
1) What's the right way to tell the Home Page to display the Partial View only on this specific case?
2) What would be the URL link look like?
3) What would the HomePage Index Controller look like so that it would also handle the possibility of a ResetPassword external link request?
You could do something simple like have a query string. for example www.yoururl.com/index?showresetpassword=true
Then inside your view you can add an if statement to render the partial view or not. If you need it inside of the controller I suggest you have the parameter as a nullable bool. Something like
public ActionResult Index(bool? showresetpassword)
Make it nullable so that if it is not inside of the url you will not have any isssues.
I am a little stuck, how would I go about moving the login form in MVC3 C# to a self created
layout page which is done in HTML so i can position the login form in my layout instead of clicking on the login link?. I have a box in my HTML design which I would like the LOGIN page to appear in. Is this possible. If there is anything you people would require please let me know in advance thanks. So in general terms when i run my MVC3 c# application my login form appears on the same page that appears first which is my layout page. My controller has the following code:
namespace TestProject.Controllers
{
public class ViewDeveloperController : Controller
{
public ActionResult Index()
{
using (var db = new GameZoneEntities5())
{
return View(db.tblDevelopers.ToList());
}
}
}
}
I dont know if that will be helpful if there is anything else please ask.
Use a Child Action for this. A Child Action is basically just like a normal action, except that it returns a PartialView instead of a View, which you can think of as a re-usable part of page with some logic behind it. Then, all you need to do is have your layout page render the child action, via the RenderAction HTML helper method.
So, you code could look something like this
public class AccountController
{
public ActionResult LogOn()
{
return PartialView();
}
}
And your _Layout.cshtml would just need to have the following code to render the child action in the place where you want your login control to appear.
#{ Html.RenderAction("LogOn", "Account" }); }
Your LogOn view razor code would just look like normal.
Once you had that working, it would be fairly easy to make some improvements, like having the LogOn Child Action show a LogOut view if the user was already logged in, or use JQuery have the LogOn form POST the results via AJAX making the UI that much nicer.
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?