Solved

mvc, razor, controller, asp.net

Posted on 2013-11-08
8
577 Views
Last Modified: 2013-11-14
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.
0
Comment
Question by:solution1368
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39635173
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
 

Author Comment

by:solution1368
ID: 39635265
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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 

Author Comment

by:solution1368
ID: 39635553
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39635796
You cannot have two methods with the same name and parameter list (or lack of)--even if the return type is different.
0
 

Author Comment

by:solution1368
ID: 39635909
so what u want me to do? I just want to add something in on load method in mvc controller.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39635951
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39638142
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question