Solved

mvc, razor, controller, asp.net

Posted on 2013-11-08
8
574 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 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
 

Author Comment

by:solution1368
ID: 39635267
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bound data table problem 2 32
How to read text with RegEx... 2 25
JSON Deserialize issue 6 26
.NET Enums [Flags] & Bitwise Design Question 6 18
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 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