I want to call a controller ActionResult method from anchor tag without changing the url in MVC3 razor engine.
My url is: "http://localhost:2993/admin/adminindex" and I have one anchor tag "SingOut" and i want to remove my session.
This is my syntax on View page:
<div>#Html.ActionLink("SingOut", "signout", "Admin")</div>
And on Admincontroller I want to call this ActionResult method
public ActionResult signout()
{
Session.Abandon();
return View("~/Views/admin/admin.cshtml");
}
When I click on signout, it always goes to Index ActionResult method.
Is it Possible to call Controller ActionResult method from javascrit not jquery?
I want it only pure javascript not jquery.
Abandoning the session will most likely cause a redirect to the index page. The page you are viewing is protected (as far as i understand) so when you sign out you cant stay on the same page. Then why would you do it asynch if the system is redirecting you anyway (or worse you are logged out and showing a page that doesnt work) ?
Related
How to get [HttpGet] and [HttpPost] method in one click event
Ex: When users click into link, then display a detail page and update status of link into database.
Any ideas for this?
I think what you are trying to say is you need a link which calls your controller (to store some stuff in some database). And then displays a detail page, right?
You can simply use #Html.ActionLink helper method to generate a link to a controller/action for you. The action should be your detail action of whatever controller you need.
Within the controller action implementation, save some stuff to the database.
You can do the database update in your GET action itself before returning the view.
public ActionResult Details(int id)
{
yourRepositary.MarkMessageAsRead(id);
var messageVM=yourService.GetMessage(id);
return View(messageVM);
}
I have a page that has a search function in a controller which works well
[HttpPost]
public virtual ActionResult Search(SearchModel model)
{
...adds to IEnumerable and such
return View(model);
}
My problem is I have another page with a search box, which I need to redirect to the same view as above. (parameters in the URL is not an option)
#using (Html.BeginForm("Search", "Home", FormMethod.Post, null))
{
#Html.TextBoxFor(t => t.SearchModel)
<input.....
}
but it's not loading up the right URL, it's just adding it to the current one. so instead of example.com/Home/Search it's adding it to the end of where that form is currently located. So if the page was in example.com/About/SearchPage its adding the t.SearchModel to the About/Searchpage
Edit: I have two different Controllers and Views, ControllerA and ViewA works fine, it brings back the search results. I want ViewB, which has an input box, to call ViewA and use ControllerA search technique
You have a nested form. This is not valid HTML. HTML does not allow you to put one form inside another form. You can have more than one form on a page, but you cannot nest them.
I'm also not sure what you mean by "another section", do you mean the #section keyword in Razor? Or do you mean an MVC area? Or something else?
On my site, I want to display a Login box on all pages. So I wanted to make a partial view page, which I use on my _Layout.cshtml file in Shared.
But where would the controller for this partial view go? And how would my Login button have access to it?
So, when the Request.IsAuthenticated is true, the login box shows 'Logged in as ...', but when the result is false, I get a little table with the usual Username/Password form.
Edit: After trying some answers below, I seem to be stuck in an endless lopp on the GET method below. It it because my partial view is trying to load me _Layout.cshtml file, as it want to accosiate the 'masterpage' with the partial view? And because my partial view is being rendered in _Layout.cshtml, it's lopping?
public class LoginController : Controller
{
//
// GET: /Login/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(LoginModel loginModel)
{
if(ModelState.IsValid)
{
var g = new GallaryImage();
var user = g.LoginUser(loginModel.Username, loginModel.Password);
if(user != null)
{
FormsAuthentication.SetAuthCookie(user.username, false);
return RedirectToAction("index", "Home");
}
ModelState.AddModelError("", "Invalid Username/Password");
}
return View();
}
In my _Layout.cshtml, I am trying to load the partial view like this:
<div style="text-align: right">
#Html.Action("Index", "Login")
</div>
See the issue?
FYI, Views don't have controllers. Controllers have Views. The distinction may seem subtle, but it's not. A view can be used by any number of controllers, and views don't care or know about the controllers. So you have to think about the current URL, which means the current Action Method.
In the case of your login partial, it doesn't need much if anything from the Controller. It's directly accessing the User property of the page to find out if it's authenticated. Your login button is just a form with it's action method set to your login method of your account controller.
Even the username can be displayed from the User property of the page.
Just look at the default MVC app that is generated when you create a new Internet project. It has all this functionality already implemented. Just copy it.
You can give controller name as attribute to Html.Action method
Html.Action("ActionName", "ControllerName")
If you use Razor syntax, you must write #Html.Action("ActionName", "ControllerName") in your view and your Action with name ActionName should return PartialView(which wil be your login area).
Try to use HTML.RenderAction("Action", "Controller") and make sure you are not calling your master page from within the partial view. This has the potential to produce a loop.
I'm using MVC 3 ASPX view engine not razor though.
I am trying to redirect to a post action result from another action result function. In this case, I would like to redirect to the Index Post, from the Summary function. Is that possible?
The index page is my search page and the Post action would return results. Should a user enter an id in the address bar, the search can be performed and the results be displayed.
public ActionResult Summary(string id)
{
//simple code
if(true)
{
return RedirectToAction("Index", "Home", HttpVerbs.Post);
}
return View();
}
See this previous answer for some context, but I would agree with the second option listed there. In your case, it might not be a third-party server, but rather your Index action that only accepts POST.
Create the form to post to your server. When the form is submitted, show the user a page that has a form in it with all of the data you want to pass on, all in hidden inputs. Just show a message like "Redirecting...". Then, add a javascript event to the page that submits the form to the third-party server.
In other words, Summary would return a View which, through JavaScript, posted to Index.
Why can't you just return a PartialView of the results back to the Index page?
I have a customer index page that displays customer lists. I have a search functionality within this page and I want the url to be http://mysite/search?id=434 when i perform the search. The index page will also display the search result.
Thanks
public class CustomerController : Controller
...
public ActionResult Search(int id)
{
ViewData["SearchResult"] = MySearchBLL.GetSearchResults(id);
return View("Index");
}
...
Hope this helps
No, you shouldn't have. Just use View("ViewName"); in the controller to show the appropriate view in other actions.
In the URL that you suggest, you should have a Controller method called 'search' and using the 'index' view for that controller.
If thats the case, you can post it back to the same action and in the Controller have different sets of code for the 'GET' and 'POST' to give the functionality that you are looking for.
Have the HTML form post to the same action that rendered it. That action can decide if it's a non-search (first visit) or a search results rendering. That action can populate ViewData with the appropriate data. That is, if you want to do that.
You can also have two views, really easily. And the action can transparently decide which one to render.