Solved

mvc, razor, controller, asp.net

Posted on 2013-11-08
8
567 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
  • 4
  • 3
8 Comments
 
LVL 74

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
 

Author Comment

by:solution1368
ID: 39635267
0
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 74

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 74

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now