Asp.net mvc controller code method struck - c#

I am relatively new to asp.net mvc but I have worked on asp.net webforms. The issue i am facing is I have button on my view page if I click certain action needs to be done and alert message has to be thrown. I could easily achieve this in asp.net webforms with a onclick event and return back to same page but I am unable to do this asp.net mvc
Action method in controller:
Public void loaddata()
{
//Some logic
Viewbag.message="success"
}
In view page:
<scripts>
#if(Viewbag.message!= null)
{
//javascript alert box logic
}
<scripts>
This code works till controller action method but after finishing that it doesn't go to the view where I have the button

Your controllen action is returning void, so nothing goes back to your view.
In MVC this is a bad practice. If you really don't want to return a view or partialView, you can also return a different type of ActionResult.
https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.actionresult?view=aspnet-mvc-5.2
Returning a HttpStatusCodeResult or JsonResult might help you in this situation.

Related

Normal post and Ajax post in MVC3

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.

How to call a Controller ActionResult method from #Html.ActionLink

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) ?

How do I add a button click event on a different class in .Net MVC?

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.

MVC3 Partial view inside Master Page

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.

ASP.NET with MVC 2 in VS2010

Hi
i want to do a work in asp.net using mvc and ajax
i have a button
when i click that button,its text should be changed.e.g before click(Click me) after click(u clicked me)
but i want to do this work in MVC2
i have studied but couldn't understood mvc
kinfly do this example so that i can understand it easily
Best Regards:
Shaahan
are u just trying to change the text label on click?
there's a few ways to do this, but you can probably just use an onclick event and change the label straight when the user click on the button.
for example like so.
but if you want to do it MVC just for the heck of it, then you can create a view, click on the button and do a form post to the same page, and on the controller use ViewData["ButtonLabel"] and update the button label when the page goes back :P
You can do what you want simply with javascript. If you want to learn mvc here a simple [music store][1] tutorial that I has helped me a lot! [1]: http://www.asp.net/mvc/tutorials/mvc-music-store-part-1
MVC stands for Model, View, Controller.
The way this works is you have a controller, say HomeController which is a class derived from Controller. When you access /Home/ on the site through your browser it serves the browser a view and any additional information, often cookies and such. The model is the data and logic of the program, often handling things like databases.
There are multiple ways to go about this example.
//In HomeController class
public ActionResult Index()
{
return View();
}
public ActionResult Clicked()
{
return View()
}
And then for the Views for the index view you'd have a button which would link to /Home/Clicked. Then on the clicked view you'd have the button with changed text.
Of course this is only one way to do it you could just append a number do the /Home/ url and pass that to the view and if it's not 0 have the test be different or use javascript to change the button's text
Index view:
<form>
<input type="button" value="NClicked" onclick="window.location.href='/Home/Clicked'">
</form>
Clicked view:
<form>
<input type="button" value="Clicked" onclick="window.location.href='/Home/'">
</form>
Of course there's more to the views than that, but you can insert that into your body.

Categories

Resources