Sunday, 15 September 2013

ASP.Net MVC4 with Razor - Best and correct way to acces controler methods in views -



ASP.Net MVC4 with Razor - Best and correct way to acces controler methods in views -

i have been working asp.net new asp.net mvc 4 razor concept. question might basic appreciate every help couldn't find definite reply days (or looking wrong things). question is: how access controller method within view without using index method?

the scenario:

i have database store values date, cost etc ... (variables defined in model.

i have controller thats sets view index

i have view shows indexpage values of info base.

i want sum values of cost colum dont want store result in db. in understanding access db via method in controller class , phone call method in view.

to larn step step defined fixed value in controller see how show value in view.

the code:

model: [range(1, 100)] [datatype(datatype.currency)] public decimal cost { get; set; } // amount of entry controller: public actionresult index() { homecoming view(db.amounts.tolist()); } public actionresult showsumofallprices() { //for testing db not called yet. query must defined , result written variable within mehod viewbag.price = 2; homecoming view(); } view: @*testmethod displaying sumprize (first wit test result) via controler*@ <table> <tr> <td>hallo @viewbag.price</td> </tr> </table>

the result not shown long don't define method content of showsumofallprices() (in controller class) in index() method.

the question is: result of method in view visible if define within index() method in controller or can write method did , phone call in view? working if c&p logic of showsumofallprices() index(). don't think need define in index method. if so, big , fat method. far see there 3 possible ways not might "nice" or working:

define in index() method. define other methods phone call them in index method because way display within view , not using model because dont want store info in db i can define other methods , straight phone call them in view without having new page result of method shown under content of currend index page.

i hope question understandable , question not asked mine before.

define in index() method.

controllers not meant hold logic.

define other methods phone call them in index method because way display within view , not using model because dont want store info in db

don't define other methods in controller itself. create other classes , phone call methods in index() or suitable controller method. , if want display info in view , model best choice, can store info in viewbag

i can define other methods , straight phone call them in view without having new page result of method shown under content of current index page.

if want fetch result of methods in same view can case[might using same kind of ajax] if want render other page , might bad idea.

the best thing can declare class required function single responsibility principle , utilize class operation in method want have results in.you can access controller methods using ajax , jquery, google out bit , own.

hope helps.

razor asp.net-mvc-4

No comments:

Post a Comment