samedi 19 avril 2014

c# - méthode d'appel du rasoir vue - Stack Overflow


I looked around and couldn't find an easy solution.


I've tried @GetUserName which doesn't work.
I've tried @ { GetUserName which doesn't work.


There has to be an easy way to call a method from the razor view engine.


It is within a foreach loop.


I need GetUserName(item.userID)


The below code is in my controller.


[ChildActionOnly]
public string GetUserName(int userID)
{
ProPit_User user = db.ProPit_User.Find(userID);

return user.username;
}



Trying to call a controller action method directly from your view is usually a sign of bad design.


You have a few options, depending on what you are trying to do:



  1. Avoid calling the method, instead put the data in your model, and render the model

  2. Use Html.RenderAction

  3. Put the method in another class and use normal function syntax.


(1) is usually my default approach, often incorporating DisplayTemplates and EditorTemplates


For (3), e.g.


public static class Util
{
public string MyUtilMethod(int blah)
}

And the view:


@Util.MyUtilMethod(1)



The obvious problem you have is that the UserName should come in as part of the model, or at least in the ViewBag.


public ActionResult MyAction(int userID) {
ViewBag.UserName = db.ProPit_User.Find(userID).username;

return View();
}

Okay, but let's say for some crazy reason you don't have the user ID at the time. Then you can go ahead and have that method in a class outside of your controller, and create an instance in your view.


@{
var db = new Db();
var userName = db.ProPit_User.Find(userID).username;
}
<div>userName is @userName</div>

It happens, but your want to have all of your data in your model and sending that to your view. You're view should only be for displaying the data, and should be going out fetching information from databases.




Although you can obtain the controller instance from your view, but doing so is just wrong:


@{
var controller = ViewContext.Controller as MyController;
var userName = controller.GetUserName(123);
}

(and in fact, if you make the controller method static, you wouldn't need the instance)


The better way to have arrived at this result is to add all the data needed by the View, such as the userName to the Model, or, as even to the ViewBag dynamic.



I looked around and couldn't find an easy solution.


I've tried @GetUserName which doesn't work.
I've tried @ { GetUserName which doesn't work.


There has to be an easy way to call a method from the razor view engine.


It is within a foreach loop.


I need GetUserName(item.userID)


The below code is in my controller.


[ChildActionOnly]
public string GetUserName(int userID)
{
ProPit_User user = db.ProPit_User.Find(userID);

return user.username;
}


Trying to call a controller action method directly from your view is usually a sign of bad design.


You have a few options, depending on what you are trying to do:



  1. Avoid calling the method, instead put the data in your model, and render the model

  2. Use Html.RenderAction

  3. Put the method in another class and use normal function syntax.


(1) is usually my default approach, often incorporating DisplayTemplates and EditorTemplates


For (3), e.g.


public static class Util
{
public string MyUtilMethod(int blah)
}

And the view:


@Util.MyUtilMethod(1)


The obvious problem you have is that the UserName should come in as part of the model, or at least in the ViewBag.


public ActionResult MyAction(int userID) {
ViewBag.UserName = db.ProPit_User.Find(userID).username;

return View();
}

Okay, but let's say for some crazy reason you don't have the user ID at the time. Then you can go ahead and have that method in a class outside of your controller, and create an instance in your view.


@{
var db = new Db();
var userName = db.ProPit_User.Find(userID).username;
}
<div>userName is @userName</div>

It happens, but your want to have all of your data in your model and sending that to your view. You're view should only be for displaying the data, and should be going out fetching information from databases.



Although you can obtain the controller instance from your view, but doing so is just wrong:


@{
var controller = ViewContext.Controller as MyController;
var userName = controller.GetUserName(123);
}

(and in fact, if you make the controller method static, you wouldn't need the instance)


The better way to have arrived at this result is to add all the data needed by the View, such as the userName to the Model, or, as even to the ViewBag dynamic.


0 commentaires:

Enregistrer un commentaire