How to call controller action on page load in asp.net mvc - asp.net-mvc

I have a view that looks like this:
http://whatever/Download/viaId/12345
And i would like to call the action
public void viaId (int Id)
{
//Code
}
when the page loads. Right now I only have the controller implemented and when I browse the url the parameter Id is null.
Do i need to create the view and call it through javascript ?

Ok i got it solved. I did not have the parameter mapped on the RouteCollection. thanks for your suggestions

It sounds like you don't have a view, you just have a URL and an action.
You should create a view called "viaId" for your Controller to give to the user. The easiest way to do this is to right click on the action and click "Add View" (assuming you're using Visual Studio).
Additionally, the viaId method should probably return ActionResult instead of void.

I think you just need to add the attribute
[AcceptVerbs(HttpVerbs.Get)]
public void viaId(int id) {}

Related

How do I create a new ActionResult in ASP.NET MVC?

If I have MVC controller that is using something like,
public ActionResult Index()
{
return View();
}
public ActionResult Display()
{
return View();
}
When I run something like this it does not seem to recognize the action for "Display", suppose I use an action link in razor for example,
#Html.ActionLink("Display", "Display")
For what whatever reason this is not routing to the new action result I created, "Display", when ActionLink is clicked. What is the proper way to do this? Do I need a separate controller for this new action?
For your example:
#Html.ActionLink("Display", "Display")
The first parameter is your linkText and the second parameter is the actionName. You really should add a third parameter for your controllerName. So, for example, this:
#Html.ActionLink("Display", "Display", "Home")
Would mean "Display" is shown as your link text, and the link would navigate to the HomeController and call the Display action method as desired.
In my example above, the resulting HTML source would look like this:
Display
("/Home" is not shown as it is implied to be to top level depending on default routing configuration)
Clear?

New view does not appear in the browser.

I am using MVC.NET, using aspx view engine. I have created a new view, at visual studio, under HOME directory named jobs.aspx.
When I go to properties, it shows "Browse to URL: ~/Home/jobs".
But When I add
Localhost:port/Home/jobs at the browser I get 404 error.
While Localhost:port/Home works normally.
Do you know how I can fix that?
Thanks
You need to add a Jobs() action to your HomeController.cs to complete the loop. Notice the Index() action in that class.
http://www.asp.net/mvc/tutorials/older-versions/getting-started-with-mvc/getting-started-with-mvc-part1
In addition to SethMW's answer.
Go to your HomeController class and add ActionResult that will return a View.
This action result should be called Jobs() and may return a specific or default view.
public ActionResult Jobs()
{
return View();
//return View("NameOfDesiredView");
}
Also if you are getting started with MVC I suggest you to learn the Razor syntax rather than the outdated aspx view engine.

Add multiple ActionName for button

I have one controller on which i have Save button click event. Im using same controller and view for Add and Edit purpose. My code is as per below
[HttpPost]
[Button(ButtonName = "Save")]
[ActionName("Create")]
[ValidateAntiForgeryToken(Salt = "PostData")]
public ActionResult Save(Ntegra m_Ntegra,FormCollection form)
{}
As Im Using ActionName("Create") here so button can not work for ActionName("Edit"). can anyone tell me how i can achive my requirnment!!
Thanks for help...... :)
You can use one view i.e Edit or Create but you would need to write 2 different Controllor actions (Create,Edit).
Please refer MVC Music store CodePlex project which will give you idea on what conventions are used and to write standard MVC code
You can have action method named Save and pass the Model. IN action method you can check if ID is exist in model or not. If there is pre defined ID you can update the record otherwise you can create new record. IN you For FormCollection, while rendering the form use hidden field to store the Id field.
public ActionResult Save(FormCollection form)
{}
It's not really required two controller actions for create and edit, you can have a single action without decorating with ActionName attribute.
In the single controller action you have to check the id of the entity going to be saved, if there is some value then it will be edit else create.
[HttpPost]
public ActionResult Save(Ntegra m_Ntegra)
{
if(m_Ntegra.Id == 0)
{
// create
}
else
{
// edit
}
}

Show partial view based on parameter

In the process of updating a C# MVC 2.0 application!
I have a view “Signup” and another view “ForgotPassword”.
Each view have a with a submit button.
Each form is submitted to the same Controller but to two different ActionResult:
[HttpPost]
public ActionResult Signup(SignupModel signupModel)
{…}
[HttpPost]
public ActionResult ForgotPwd(ForgotPasswordModel forgotPasswordModel)
{…}
Upon completion my goal is to redirect the user to a “thankyou” page but based on where the user is coming from (either Signup or ForgotPassword) I wish to display a particular message (or a different UI).
Inside the same Controller, I created a “Thankyou” ActionResult:
public ViewResult Thankyou()
{
return View();
}
I was thinking of adding a parameter to my Thankyou() method which would allow me to know where the user is coming from (Signup or ForgotPwd). From there, make the “thankyou” page display the appropriate UI/message.
I’m looking for a clean and simple solution.
Should I create two View User Controls and show the appropriate one based on the parameter being passed?
In addition, instead of having an “ActionResult” for my Thankyou() method couldn’t I use a “PartialViewResult” ?
EDIT:
I was actually considering something along those lines…
Where ThankyouType is an Enum.
[HttpPost]
public ActionResult Signup(SignupModel signupModel)
{
//Validation code...
return View("Thankyou", ThankyouType.SignupDone);
}
[HttpPost]
public ActionResult ForgotPassword(ForgotPasswordModel forgotPasswordModel)
{
//Validation code...
return View("Thankyou", ThankyouType.ForgotPasswordDone);
}
And then have my “Thankyou” ViewResult like this:
public ViewResult Thankyou(ThankyouType type)
{
return View(type);
}
Doesn’t seem like I can create a strongly typed view based on Enum (unless I’m wrong).
Perhaps I’ll read more on PartialViewResults and/or find examples…but then again, I could be completely wrong.
I would personally give the ThankYou view a model that has the message you want to display, and have your two controller actions render the ThankYou view directly on success rather than calling a ThankYou action.
However, if you're sure you want a redirect, you may consider using the TempData collection to store a message or a key of some kind. The ThankYou controller can then retrieve this value and pass it to the View. This situation is what TempData was made for.
Edit
There's no reason you shouldn't be able to use an enum value as your model type, but if that gives you trouble you should at least be able to create a model type that has an enum property on it.
The strategy of sending the ThankYouType as part of the redirect request would work just fine, if that's what you prefer. The only potential downside is that it would look like this in the URL:
http://domain.com/controller/ThankYou?type=ForgotPasswordDone
I have no real arguments against it. There are lots of options. Use the one that feels best to you.

ASP.NET MVC - Go to a different view without changing URL

is it possible to go to a different View without changing the URL? For example in my Index View, I have a link to go the the Details View but I would like to keep the URL the same.
Thank you very much,
Kenny.
As already mentioned, you could make the Details link an Ajax.ActionLink and use this to change the content of a div.
Failing that, the only other way I can think of doing it is by making your details link a button and POST to your index action. You could apply CSS to the button to make it appear more like a normal html link.
public class HomeController : Controller {
public ActionResult Index() {
return View("Index");
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(int hiddenInputFieldId) {
return View("Details");
}
}
EDIT:
Based on JonoW's comment, you'll have to pass in a 'fake' param with your post, this is not really a problem though, you can just use a hidden input field for it.
You can return the same view from multiple controller actions, but each controller action requires a unique URL:
public class HomeController : Controller {
public ActionResult Index() {
return View("home");
}
public ActionResult About() {
return View("home");
}
}
If you want a link to load up content from a different page without changing the URL, you'll have to use some Ajax to call the server for the content and update the parts of the page you need to change with the new content.
I don't know why you would like to do that, but you could have an Ajax.Actionlink which renders the Details View..
There is almost no reason to hide an URL, not sure what you would like to to.. maybe you explain further that someone can give a better approach.
You can use a good old Server.Transfer for this. However, I'd suggest doing it like has been detailed in this SO post. This gives you an easy way to return an ActionMethod from your current action without peppering your code with Server.Transfer() everywhere.
You can do this by rendering partials- I do this to load different search screens. Sample code is as follows (this is slightly different to my actual code, but you'll get the idea):
<% Html.RenderPartial(Model.NameOfPartialViewHere, Model.SomeVM); %>
Personally though, I don't see why you don't just change the URL?

Resources