mvc, razor, controller, asp.net

I just want to know if I want to default some value on load event in mvc 3 controller.
how should i do? I am new to mvc, and i am web form .net developer.

getting used to on load event.

However, I want to code in controller instead of view.

Please show me how to do that.
solution1368Asked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
Put the default data in your model within the default Index action (if that's what you're using as your default). Then simply pass that data to the view.

e.g.

public ViewResult Index()
{
    YourModel model = new YourModel();

    model.TheProperty = "the default value";

    return View(model);
}

Open in new window


Be sure to set up your view to expect a model of that type:

@model YourModel

<!-- some HTML -->

<div>@Model.TheProperty</div>

Open in new window


Note the difference in casing of "model" when you are declaring the model type (little "m") and actually using the model instance (big "M").
0
 
solution1368Author Commented:
got it. Thanks
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_28289502.html

need more helps above as well if u are available.
0
 
solution1368Author Commented:
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
solution1368Author Commented:
not working. It alerts duplicated below

  public ActionResult Index()
        {

            return View();
        }


public ViewResult Index()
{
   //  assume i just want to show  below when it is on load
      Response.write("Hello");
     return View();
}
0
 
käµfm³d 👽Commented:
You cannot have two methods with the same name and parameter list (or lack of)--even if the return type is different.
0
 
solution1368Author Commented:
so what u want me to do? I just want to add something in on load method in mvc controller.
0
 
käµfm³d 👽Commented:
It's what I showed you in my first post. The only difference is that you created an entirely new method, whereas I suggested modifying the existing method.
0
 
Carl TawnSystems and Integration DeveloperCommented:
MVC doesn't work the same way as WebForms. There are no "events" in the same sense that there are in WebForms.

Controller methods respond to GET and POST requests on a route, and render a View passing it a Model containing the data to be displayed. There is no "Load" event because the controller is not a Page, and does not have any events.

So, as kaufmed has already stated, to do what you want you need to pass a model to a View which is returned by the default method of your default controller.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.