?
Solved

mvc, razor, controller, asp.net

Posted on 2013-11-08
8
Medium Priority
?
580 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 2000 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
Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

762 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